forked from agordon/d3export_demo
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.js
78 lines (64 loc) · 2.21 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//?
var express = require('express'),
app = express(),
connect = require('connect'),
sys = require('sys'),
tmp = require('tmp'),
fs = require('fs'),
exec = require('child_process').exec;
/**
* Configuration
*/
app.use(express.bodyParser());
app.use(connect.compress());
app.use(express.methodOverride());
app.use(app.router);
app.set('views', __dirname);
app.engine('html', require('ejs').renderFile);
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
app.get('/', function (req, res)
{
res.render('index.html');
});
app.post('/', function(req, res) {
// output format (pdf or png )
tmp.file({postfix: '.svg'}, function _tempFileCreated(err, inputFilePath, fd) {
if (err) {
res.json(500, err);
} else {
fs.writeFile(inputFilePath, req.body.data, function(err) {
if (err) {
res.json(500, err);
} else {
tmp.file({postfix: '.'+req.body.output_format}, function _tempFileCreated(err, outputFilePath, fd) {
if (err) {
res.json(500, err);
} else {
var cmd = "rsvg-convert -z 5 --background-color white -a";
cmd += " -f "+req.body.output_format;
cmd += " -o "+outputFilePath;
cmd += " "+inputFilePath;
exec(cmd, function (error, stdout, stderr) {
if (error !== null) {
res.json(500, error);
} else {
res.attachment(outputFilePath);
res.sendfile(outputFilePath);
}
});
}
});
}
});
}
});
});
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log('Listening on http://localhost:'+ port);
});