快速节点与python的通信

时间:2015-12-07 11:23:10

标签: javascript python node.js web-services express

我有一个快速节点应用程序,其中包含一个表单,该表单接受用户数据,提供给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")
});

2 个答案:

答案 0 :(得分:1)

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");
});

答案 1 :(得分:0)

这是我身边一个非常愚蠢的错误。谢谢@Hiren .S。我没有正确地放置python文件的路径。在上面的代码中,它必须是./long.py用于同一目录。它只是滑动了我的眼睛。