-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (59 loc) · 1.81 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
// Simple HTTP server that renders barcode images using bwip-js.
const http = require('http');
const bwipjs = require('bwip-js');
const DrawingSVG = require('./drawing-svg');
var url = require('url');
// This shows how to load the Inconsolata font, supplied with the bwip-js distribution.
// The path to your fonts will be different.
//bwipjs.loadFont('Inconsolata', 100,
// require('fs').readFileSync('./fonts/Inconsolata.otf', 'binary'));
http.createServer(function(req, res) {
// If the url does not begin /?bcid= then 404. Otherwise, we end up
// returning 400 on requests like favicon.ico.
if (req.url.indexOf('/?bcid=') != 0) {
res.writeHead(404, { 'Content-Type':'text/plain' });
res.end('BWIPJS: Unknown request format. http://192.168.1.22:3030/?bcid=code39&text=DJD545484&includetext', 'utf8');
} else {
// bwipjs.request(req, res); // Executes asynchronously
requestSvg(req, res);
}
}).listen(3030, "0.0.0.0");
function requestSvg(req, res, extra) {
var opts = url.parse(req.url, true).query;
// Convert boolean empty parameters to true
for (var id in opts) {
if (opts[id] === '') {
opts[id] = true;
}
}
// Add in server options/overrides
if (extra) {
for (var id in extra) {
opts[id] = extra[id];
}
}
ToBuffer(opts, function(err, svg) {
if (err) {
res.writeHead(400, { 'Content-Type':'text/plain' });
res.end('' + (err.stack || err), 'utf-8');
} else {
res.writeHead(200, { 'Content-Type':'image/svg+xml' });
res.end(svg, 'binary');
}
});
}
function ToBuffer(opts, callback) {
try {
bwipjs.fixupOptions(opts);
var svg = bwipjs.render(opts, DrawingSVG(opts, bwipjs.FontLib))
callback(null, svg);
} catch (e) {
if (callback) {
callback(e);
} else {
return new Promise(function(resolve, reject) {
reject(e);
});
}
}
}