Skip to content

Instantly share code, notes, and snippets.

@yurinnick
Created November 29, 2013 13:28
Show Gist options
  • Save yurinnick/7705694 to your computer and use it in GitHub Desktop.
Save yurinnick/7705694 to your computer and use it in GitHub Desktop.
Description: nodejs run specified file with specified interpretator(http://localhost:8000/run?cmd=[python/ruby]&file=[filepath])
var express = require('express');
var app = express();
function execute(inter, file, callback) {
var spawn = require('child_process').spawn;
var command;// = spawn('/usr/bin/python', [ file ]);
switch(inter) {
case "python":
command = spawn("/usr/bin/python", [ file ]);
break;
case "ruby":
command = spawn("/usr/bin/ruby", [ file ]);
break;
default:
return callback("<b>Error! Undefined interpretator!</b>");
}
var result = '';
console.log(file);
command.stdout.on('data', function(data) {
console.log(data);
result += data.toString();
});
command.on('close', function(code) {
return callback(result);
});
}
app.get("/run", function(req, res) {
execute(req.query.cmd, req.query.file, function(result) {
res.send(result);
});
});
app.listen(8000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment