我有一个 Express Node 应用程序,其中包含一个接受用户数据、输入到 mongodb 中的表单,然后运行一个以用户数据作为参数并将数据返回到应用程序的 python 算法。问题是 python 代码没有运行并且不返回任何东西。我正在使用 child_process 来运行 python 代码。请帮忙
var express = require('express');
var ws = express();
var bodyParser = require('body-parser');
var databaseUrl = "localhost:27017/ws";
var collections = ["Users"];
var mongojs = require('mongojs');
var db = mongojs(databaseUrl,collections);
var spawn = require('child_process').spawn;
ws.set('view engine','ejs');
ws.use(bodyParser.urlencoded({extended : false}));
ws.get('/',function(req,res){
res.render('default');
});
ws.use(express.static(__dirname + '/public'));
ws.get('/*',function(req,res){
res.send("no where to go..")
});
ws.post('/login',function(req,res){
var user_login=req.body.login;
var user_location=req.body.location;
var user_email=req.body.email;
console.log("User name = "+user_login+", location is "+user_location+",
Email-id is "+user_email);
db.Users.insert({login : user_login, location : user_location, Email :
user_email})
var process = spawn('python',['/long.py']);
process.stdout.on('data', function (data){console.log("python script ends
now..!!!")});
console.log("escaped python");
res.end("yes");
});
ws.listen(3000,function(){
console.log("listening on the port 3000")
});
最佳答案
var process = spawn('python', ['/long.py']);
process.stdout.on('data', function(data) {
console.log("python script ends now..!!!");
// will print process output
});
process.stderr.on('data', function(data) {
console.log('stderr: ' + data);
// Error occurred;
});
process.on('close', function(code) {
console.log('child process exited with code ' + code);
res.end("yes");
});
关于javascript - Express Node 与python的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34132384/