-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (51 loc) · 1.65 KB
/
index.js
File metadata and controls
81 lines (51 loc) · 1.65 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Calling the packages that we need
const express = require("express");
const app = express();
const port = 5000;
const bp = require("body-parser");
const qr = require("qrcode");
// Using the ejs (Embedded JavaScript templates) as our template engine
// and call the body parser - middleware for parsing bodies from URL
// - middleware for parsing json objects
app.set("view engine", "ejs");
app.use(bp.urlencoded({ extended: false }));
app.use(bp.json());
// Simple routing to the index.ejs file
app.get("/", (req, res) => {
res.render("index");
});
// Blank input
// Incase of blank in the index.ejs file, return error
// Error - Empty Data!
app.post("/scan", (req, res) => {
const url = req.body.url;
if (url.length === 0) res.send("Empty Data!");
qr.toDataURL(url, (err, src) => {
if (err) res.send("Error occured");
res.render("scan", { src });
});
});
// Setting up the port for listening requests
app.listen(port, () => console.log("Server at 5000"));
// Creating the data
let data = {
name:"Employee Name",
age:27,
department:"Police",
id:"aisuoiqu3234738jdhf100223"
}
// Converting the data into String format
let stringdata = JSON.stringify(data)
// Print the QR code to terminal
qr.toString(stringdata,{type:'terminal'},
function (err, QRcode) {
if(err) return console.log("error occurred")
// Printing the generated code
//console.log(QRcode)
})
// Converting the data into base64
qr.toDataURL(stringdata, function (err, code) {
if(err) return console.log("error occurred")
// Printing the code
//console.log(code)
})