-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (39 loc) · 1.17 KB
/
index.js
File metadata and controls
58 lines (39 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import ejs from 'ejs';
import bodyParser from 'body-parser';
import express from 'express';
import qr from 'qr-image';
import fs from 'fs';
import { dirname } from "path";
import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
const port = 3000;
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended : true}));
app.get("/",(req,res) => {
res.render("index.ejs");
})
app.post("/check",(req,res) => {
const localUrl = req.body.url;
generateCode(localUrl);
res.render("index.ejs",{
qrcode : localUrl
});
})
app.listen(port, ()=>{
console.log(`Server is running on port ${port}`);
})
app.get('/download/image', (req, res) => {
const imagePath = '/public/qr_img.png';
res.setHeader('Content-Disposition', 'attachment; filename=qr_img.png');
res.setHeader('Content-Type', 'image/png');
res.sendFile(__dirname + imagePath);
});
function generateCode(url){
var qr_svg = qr.image(url);
qr_svg.pipe(fs.createWriteStream('public/qr_img.png'));
fs.writeFile('URL.txt', url, (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
}