-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
49 lines (45 loc) · 1.46 KB
/
server.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
var connect = require("connect"),
serveStatic = require("serve-static"),
compression = require("compression"),
http = require("http"),
d3 = require("d3"),
jsdom = require("jsdom"),
fs = require("fs"),
phantom = require("phantom"),
url = require("url"),
chart = require("./chart"); //this need to be dynamic
var app = connect(),
port = 1337,
htmlStub = "<html><body><div id=\"main\"></div></body></html>",
root = serveStatic("./"),
file;
app.use(root);
app.use(compression());
app.use("/viz", function(req, res){
var qStrings = url.parse(req.url, true).query;
jsdom.env({
features : {
QuerySelector: true
},
html: htmlStub,
done: function(errors, window) {
file = chart.init(window, qStrings);
if (qStrings.mode === "raw") {
res.end(file);
} else if (qStrings.mode === "rendered") {
phantom.create(function (ph) {
ph.createPage(function (page) {
page.setContent(file);
page.renderBase64("png", function(data) {
res.setHeader("Content-Type", "image/png");
res.end(new Buffer(data, "base64"));
ph.exit();
});
});
});
}
}
});
});
app.listen(port);
console.log("Listening on " + port);