forked from LalithKumar77/QRGen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
39 lines (34 loc) · 943 Bytes
/
server.js
File metadata and controls
39 lines (34 loc) · 943 Bytes
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
import express from "express";
import { fileURLToPath } from "url";
import { dirname } from "path";
import bodyParser from "body-parser";
import qr from "qr-image";
const dir = dirname(fileURLToPath(import.meta.url));
const port = 3000;
const app = express();
app.use(express.static(dir));
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", (req, res) => {
res.sendFile(dir + "/index.html");
});
app.post("/submit", (req, res) => {
const text = req.body.text;
const qrCode = qr.image(text, {
type: "png",
size: 10,
margin: 2,
});
const buffer = [];
qrCode.on("data", (chunk) => {
buffer.push(chunk);
});
qrCode.on("end", () => {
const imageData = Buffer.concat(buffer);
res.set("Content-Type", "image/png");
res.set("Content-Disposition", "inline; filename='qr_code.png'");
res.send(imageData);
});
});
app.listen(port, () => {
console.log(`http://localhost:${port}`);
});