-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (59 loc) · 1.52 KB
/
app.js
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
var express = require('express')
var cookieParser = require('cookie-parser')
var logger = require('morgan')
const { check } = require('express-validator')
const QRCode = require('qrcode')
const config = require('./config.json')
var app = express()
app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.set('views', './views')
app.set('view engine', 'ejs')
app.get('/', function (req, res) {
res.redirect(config.default)
})
app.get(
'/:marker',
[check('marker').isString().trim().escape()],
function (req, res) {
const marker = req.params.marker
if (
marker !== undefined &&
typeof marker === 'string' &&
config.redirects[marker] !== undefined
)
res.redirect(config.redirects[req.params.marker])
else res.json({ result: 'ERROR', message: 'Missing or invalid marker' })
}
)
app.get(
'/:marker/qr',
[check('marker').isString().trim().escape()],
function (req, res) {
const marker = req.params.marker
if (
marker !== undefined &&
typeof marker === 'string' &&
config.redirects[marker] !== undefined
) {
QRCode.toDataURL(
config.redirects[marker],
{ size: 500, scale: 25, qzone: 5 },
function (err, url) {
if (err) {
console.log('error: ' + err)
res.json({
result: 'ERROR',
message: 'Error while generating QR code'
})
}
res.render('qr', { marker, url })
}
)
} else
res.json({ result: 'ERROR', message: 'Missing or invalid marker' })
}
)
module.exports = app