-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
46 lines (40 loc) · 1.27 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
var express = require('express')
, http = require('http')
, gm = require('gm')
, app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.use(express.logger('dev'));
app.use(express.favicon());
app.use(express.bodyParser());
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/:width([0-9]+)x:height([0-9]+)', function(req,res,next) {
var height, width, _ref, text, bg, fg, fs;
_ref = [parseInt(req.params.width)
, parseInt(req.params.height)]
, width = _ref[0]
, height = _ref[1]
, text = ('text' in req.query) ? req.query.txt : (width + 'x' + height)
, bg = ('bg' in req.query) ? req.query.bg : '#aaa'
, fg = ('fg' in req.query) ? req.query.fg : '#fff'
, fs = ('fs' in req.query) ? req.query.fs : 40;
gm(width, height, bg)
.fontSize(fs)
.fill(fg)
.font('SourceCodePro-Light.ttf')
.drawText(0, 0, text, 'center')
.stream('png', function(err, stdout) {
if (err) {
console.log(err);
process.exit();
}
stdout.pipe(res);
});
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});