-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (32 loc) · 1.19 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
var http = require('http'),
url = require('url'),
tache = require('mustache');
fs = require('fs');
http.createServer(function (request, response) {
console.log('Request started');
response.writeHead(200, {'Content-Type': 'text/plain'});
fs.readFile('./script.json', 'utf8', function(err, data) {
if (err) {
console.log('Error reading');
response.write("Couldn\'t read script");
response.end();
return;
}
var urlParts = url.parse(request.url, true).query;
var script = JSON.parse(tache.render(data, urlParts));
var spawn = require('child_process').spawn,
proc = spawn(script.script, (script.args ? script.args : []));
proc.stdout.on('data', function (data) {
response.write('stdout: ' + data.toString());
});
proc.stderr.on('data', function (data) {
response.write('stderr: ' + data.toString());
response.end('end');
});
proc.on('close', function(code) {
response.write('closed with code 2' + code);
response.end('end');
});
});
}).listen(8080);
console.log('Server started');