-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
70 lines (60 loc) · 1.98 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import express from "express";
import bodyParser from "body-parser";
import qr from "qr-image";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { dirname } from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
const PORT = process.env.PORT || 3000;
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, "public")));
// Endpoint to generate QR code
app.post("/generate", (req, res) => {
const url = req.body.url;
console.log(`Received URL: ${url}`);
// Fixed filename for the QR code image
const qrFilename = "qr_img.png";
const qrPath = path.join(__dirname, "public", qrFilename);
const qrPng = qr.image(url, { type: "png" });
const writeStream = fs.createWriteStream(qrPath);
qrPng.pipe(writeStream);
writeStream.on("finish", () => {
// Append the URL to URL.txt
fs.appendFile(
path.join(__dirname, "public", "URL.txt"),
url + "\n",
(err) => {
if (err) {
console.error("Error saving URL:", err);
return res.status(500).send("Server error");
} else {
console.log("The URL has been appended and QR code generated!");
return res.status(200).json({ qrCodeUrl: `/${qrFilename}` });
}
}
);
});
writeStream.on("error", (err) => {
console.error("Error generating QR code:", err);
return res.status(500).send("Server error");
});
});
// Endpoint to get the list of URLs
app.get("/urls", (req, res) => {
const filePath = path.join(__dirname, "public", "URL.txt");
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
console.error("Error reading URL.txt:", err);
return res.status(500).send("Server error");
} else {
const urls = data.split("\n").filter((url) => url.trim() !== "");
return res.status(200).json({ urls });
}
});
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});