-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
63 lines (53 loc) · 1.59 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
// Packages
var express = require('express')
, morgan = require('morgan')
, qrCode = require('qrcode')
, properties = require('./properties')
, marked = require('marked')
, fs = require('fs')
, join = require('path').join
, app = express()
, UberCache = require('uber-cache')
, cache = new UberCache(1000)
, uberMemoize = require('uber-memoize')
, memoize = uberMemoize('QR', cache)
, createQr = memoize(qrCode.toDataURL, 10 * 60000)
app.use(morgan('combined'))
// Index route
app.get('/', function (req, res, next) {
fs.readFile(join(__dirname, '/Readme.md'), 'utf-8', function (err, data) {
if (err) return next(err)
res.write(marked.parse(data))
res.end()
})
})
// Version 1 Api Route
app.get('/v1/', function (req, res, next) {
res.write(properties.name + ' Api Version 1')
res.end()
return next()
})
app.get('/v1/:format', function (req, res, next) {
const { data, width, scale } = req.query
// Check format
if (req.params.format !== 'qr') {
return next(new Error('Incorrect format type'))
}
// Check data
if (data === undefined) {
return next(new Error('GET data is missing'))
}
// We create our QR Code
createQr(data, { width, scale }, function (err, base64Image) {
if (err) return next(err)
var image = Buffer.from(base64Image.substring(22), 'base64')
// Return buffer
res.set({ 'Content-Type': 'image/png', 'Content-Length': image.length })
res.write(image)
res.end()
})
})
// Starting Listening
app.listen(properties.port, function () {
console.log(properties.name + ' starting on ' + properties.port)
})