Spaces:
Paused
Paused
| const express = require("express"); | |
| const { CaptchaGenerator } = require("captcha-canvas"); | |
| const fs = require("fs"); | |
| const os = require("os"); | |
| const path = require("path"); | |
| const crypto = require("crypto"); | |
| const app = express(); | |
| app.set('json spaces', 4) | |
| const tempDir = os.tmpdir(); | |
| function generateRandomHex(length) { | |
| return crypto.randomBytes(length).toString("hex"); | |
| } | |
| app.use("/tmp", express.static(tempDir)); | |
| app.all("/", async (req, res) => { | |
| const captcha = new CaptchaGenerator({ height: 200, width: 600 }) | |
| const fileName = `temp_${generateRandomHex(8)}.png`; | |
| const filePath = path.join(tempDir, fileName); | |
| const buffer = await captcha.generate() | |
| fs.writeFile(filePath, buffer, (err) => { | |
| if (err) return res.status(500).json({ error: "Failed to save captcha" }); | |
| res.json({ | |
| answer: captcha.text, | |
| url: `https://${req.hostname}/tmp/${fileName}` | |
| }); | |
| }); | |
| }); | |
| app.listen(7860, () => console.log("Server running on port 7860")); |