-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
47 lines (38 loc) · 1.25 KB
/
main.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
var express = require('express');
var nunjucks = require('nunjucks');
var url = require('url');
var path = require('path');
var exec = require('child_process').exec;
var phantomjs = require('phantomjs-prebuilt');
var app = express();
var baseURL = 'http://localhost:3000';
if (process.env.ENV === 'production') {
baseURL = 'https://get-diagram.herokuapp.com';
}
app.use(express.static('public'));
nunjucks.configure('views', {
autoescape: true,
express: app
});
app.get('/:type', function (req, res) {
var type = req.params.type;
var url = req.url;
var query = url.substr(url.indexOf('?')+1);
var cmd = phantomjs.path + ' rasterize.js \'' + baseURL + '/render/' + type + '?' + query + '\' ';
// TODO: Cache in a way that will work on Heroku
exec(cmd, function (err, stdout, stderr) {
var img = new Buffer(stdout, 'base64');
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': img.length
});
res.end(img);
});
});
app.get('/render/:type', function (req, res) {
var type = req.params.type;
var url = req.url;
var query = url.substr(url.indexOf('?')+1);
res.render(type + '.njk', { query: query });
});
app.listen(process.env.PORT || 3000);