-
-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathserver.js
More file actions
42 lines (37 loc) · 1.17 KB
/
server.js
File metadata and controls
42 lines (37 loc) · 1.17 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
// const express = require("express");
// const inquirer = require("inquirer");
// const qr = require("qr-image");
// const fs = require("fs");
import express from 'express'
import inquirer from "inquirer";
import qr from "qr-image";
import fs from "fs";
import cors from "cors";
const app = express();
const port = 3000;
const corsOptions = {
origin: '*',
credentials: true, //access-control-allow-credentials:true
optionSuccessStatus: 200,
}
app.use(cors(corsOptions))
app.use(express.static("public"));
app.use(express.json());
app.post("/generate_qr", async (req, res) => {
const url = req.body.url;
const qr_svg = qr.image(url);
const qrImagePath = `public/qr_img.png`;
qr_svg.pipe(fs.createWriteStream(qrImagePath));
fs.writeFile("public/URL.txt", url, (err) => {
if (err) {
console.error("Error writing URL.txt:", err);
res.status(500).json({ error: "Internal Server Error" });
} else {
console.log("URL.txt has been saved!");
res.json({ qr_image: qrImagePath });
}
});
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});