我想生成一个子进程来从节点运行python脚本。我有以下要求:从节点生成子进程运行python脚本返回500
/webcrawler?source=http://www.pygamers.com&method=BFS&nodeCount=3&depth=0&keyword=game
我已验证我的参数是否正确进入。这里是我的代码设置来处理app.js
请求:
app.get('/webcrawler', function(req, res){
var python = require('child_process').spawn(
'python',
["WebCrawler/Webcrawler.py"
, req.query.source
, req.query.method
, req.query.nodeCount
, req.query.depth
, req.query.keyword]
);
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);
});
});
我呼吁我的Python脚本,Webcrawler.py
这是在WebCrawler的目录中。 WebCrawler目录与app.js
位于同一个目录中。
然而,这个要求给了我一个500,我一直没能弄清楚为什么。好像我必须错误地生成子进程。我使用this answer作为这样做的模型。
要么尝试删除Webcrawler作为您指示节点的路径的一部分,要么给它一个绝对路径将是我的猜测 - 但我不知道节点一切都好。知道500是什么也许是有用的。 –
检查您的python脚本是否以您的目标退出码退出。否则,检查也作为结果参数传递给关闭事件的信号。用两个参数做一些进一步的分析来检查你的代码的有效性。请参阅https://nodejs.org/api/child_process.html#child_process_event_close – DmiN