-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (33 loc) · 1.16 KB
/
index.js
File metadata and controls
53 lines (33 loc) · 1.16 KB
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
import express from "express";
import fs from "fs";
import qr from "qr-image";
import cors from "cors";
import bodyParser from "body-parser";
import { fileURLToPath } from "url";
import { dirname } from "path";
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
const PORT = 3000;
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/public/index.html");
});
app.post("/generate", (req, res) => {
const url = req.body.urls;
if (!url) {
return res.status(400).json({ error: "URL is required" });
}
const qrsvgimg = qr.image(url, { type: "png" });
const qrpath = "Qr-img.png";
qrsvgimg.pipe(fs.createWriteStream(qrpath));
fs.writeFile("URL.txt", url, (err) => {
if (err) console.error("Error saving URL:", err);
else console.log("URL saved successfully.");
});
res.setHeader("Content-Type", "image/png");
qrsvgimg.pipe(res);
//sending the qr to the client
});
app.listen(PORT, () => console.log(`Server running at http://localhost:${PORT}`));