Created
November 29, 2013 13:28
-
-
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])
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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