-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (43 loc) · 1.55 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
const express = require('express')
const cookieParser = require('cookie-parser')
const logger = require('morgan')
const { check } = require('express-validator')
const QRCode = require('qrcode')
const 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')
const config = require('./lib/config')
app.get('/', function (req, res) {
res.redirect(config.wikiUrl)
})
app.get('/:page', [check('page').isString().trim().escape()], function (req, res) {
const { page } = req.params
if (page !== undefined && typeof page === 'string') {
res.redirect(config.wikiUrl + '?id=' + page)
} else {
res.json({ result: 'ERROR', message: 'Missing or invalid page' })
}
})
app.get('/:page/qr', [check('page').isString().trim().escape()], function (req, res) {
const { page } = req.params
const { baseUrl, wikiUrl, qrCodeOptions } = config
if (page !== undefined && typeof page === 'string') {
QRCode.toDataURL(`${wikiUrl}?id=${page}`, qrCodeOptions, (err, qrImgData) => {
if (err) {
console.log('error: ' + err)
res.json({
result: 'ERROR',
message: 'Error while generating QR code'
})
}
res.render('qrinfo', { baseUrl, wikiUrl, page, qrImgData })
})
} else {
res.json({ result: 'ERROR', message: 'Missing or invalid page' })
}
})
module.exports = app