Skip to content

Instantly share code, notes, and snippets.

@bobrik
Created June 28, 2013 13:21
Show Gist options
  • Save bobrik/5884601 to your computer and use it in GitHub Desktop.
Save bobrik/5884601 to your computer and use it in GitHub Desktop.
(function() {
var http = require("http"),
child = require("child_process"),
url = require("url"),
stream = require("stream");
http.createServer(function(req, res) {
var params = url.parse(req.url, true),
args = params.query.args ? JSON.parse(params.query.args) : [],
cmd = params.pathname.substr(1),
command = child.spawn(cmd, args),
output = new stream.PassThrough();
command.on("error", function(error) {
res.writeHead(500, {"Content-type": "text/plain"});
res.end((error ? error + "" : ":(") + "\n");
});
res.writeHead(200, {"Content-type": "text/plain"});
output.pipe(res);
command.stdout.pipe(output);
command.stderr.pipe(output);
}).listen(12000);
// curl 'http://127.0.0.1:12000/uname'
// curl 'http://127.0.0.1:12000/uname?args=\["-a"\]'
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment