see child_process. here is an example using spawn
, which allows you to write to stdin and read from stderr/stdout as data is output. If you have no need to write to stdin and you can handle all output when the process completes, child_process.exec
offers a slightly shorter syntax to execute a command.
// with express 3.x
var express = require('express');
var app = express();
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(app.router);
app.post('/upload', function(req, res){
if(req.files.myUpload){
var python = require('child_process').spawn(
'python',
// second argument is array of parameters, e.g.:
["/home/me/pythonScript.py"
, req.files.myUpload.path
, req.files.myUpload.type]
);
var output = "";
python.stdout.on('data', function(data){ output += data });
python.on('close', function(code){
if (code !== 0) {
return res.send(500, code);
}
return res.send(200, output);
});
} else { res.send(500, 'No file found') }
});
require('http').createServer(app).listen(3000, function(){
console.log('Listening on 3000');
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…