This repository has been archived by the owner on Dec 22, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·68 lines (51 loc) · 1.6 KB
/
index.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
'use strict';
var express = require('express');
var JsBarcode = require('jsbarcode');
var Canvas = require("canvas");
function generateEAN13(barcode) {
var canvas = new Canvas();
JsBarcode(canvas)
.options({font: "OCR-B"}) // Will affect all barcodes
.EAN13(barcode, {fontSize: 18, textMargin: 0})
.render();
return canvas.toBuffer();
}
const urlHandler = [];
urlHandler.index = function(req, res) {
res.contentType('text/html');
res.send("Welcome to <a href=\"https://github.com/theel0ja/ean13-server\">ean13-server</a>");
}
urlHandler.error404 = function(req, res) {
res.contentType('text/plain');
res.status(404);
res.send("Not Found");
}
urlHandler.ean = function(req, res) {
var ean = req.params["ean"];
if(ean.length == 13) {
res.contentType('image/png');
res.send( generateEAN13(ean) );
}
else {
urlHandler.error404(req, res);
}
}
// -- Configure Express --------------------------------------------------------
const app = express();
app.disable('x-powered-by');
// -- Routes -------------------------------------------------------------------
app.get('/', urlHandler.index);
app.get('/:ean', urlHandler.ean);
// -- Error handlers -----------------------------------------------------------
app.use((req, res) => {
res.status(404);
urlHandler.error404(req, res);
});
// -- Server -------------------------------------------------------------------
if (app.get('env') !== 'test') {
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log('Listening on port ' + port);
});
}
module.exports = app;