-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (54 loc) · 1.84 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
69
70
#!/usr/bin/env node
var logger = require('log4js').getLogger('svg-stubs');
var app = require('express')();
logger.level = "info";
var port = 8080;
if (process.argv[2]) {
port = Number(process.argv[2]);
}
var svg = function(req, res) {
var width = 1180;
var height = 90;
var text = 'hello';
var fill = '#C13C41';
var fontSize = 24;
if (req.query.width) {
width = Number(req.query.width);
}
if (req.query.height) {
height = Number(req.query.height);
}
if (req.query.text) {
text = String(req.query.text);
}
if (req.query.fill) {
fill = '#' + String(req.query.fill);
}
if (req.query.time) {
var date = new Date();
text = text + ' [' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds() + '.' + date.getMilliseconds() + ']';
}
if (req.query.fontSize) {
fontSize = Number(req.query.fontSize);
}
var textX = width / 2;
var textY = height / 2;
var result = '<svg width="' + width + '" height="' + height + '" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg"> \
<g> \
<rect height="' + height + '" width="' + width + '" id="MyRect" fill="' + fill + '"/>\
<text stroke="white" xml:space="preserve" text-anchor="middle" font-family="serif" font-size="' + fontSize + '" y="' + textY + '" x="' + textX + '" stroke-width="0" fill="white">' + text + '</text> \
</g> \
</svg>';
res.append('Content-Type', 'image/svg+xml')
.append('Cache-Control', 'no-cache, no-store, must-revalidate')
.append('Pragma', 'no-cache')
.append('Expires', 0)
.send(result)
.end();
};
app.get('/*.svg', svg);
app.get('/svg', svg);
app.get('/', svg);
app.listen(port, function() {
logger.info("listen on %s", port);
});