我正在使用python shell在nodeJS上运行python。我需要返回python shell out put作为响应。但是当我尝试发送响应时,它显示了一个错误
Can't set headers after they are sent.以下是我的代码
app.post('/therun', (req, res)=>{
var pyshell = new PythonShell('hello.py');
pyshell.on('message', function (message) {
console.log(message); //value is correct
res.send(message); //error here
});
res.send(message); //if i use here message undefined
});如何解决这个问题?
发布于 2018-07-30 14:06:16
app.post('/therun', (req, res)=>{
var pyshell = new PythonShell('hello.py');
var test={};
pyshell.on('message', function (message) {
console.log(message); //value is correct
//res.send(message); //error here
test=message;
});
console.log(test);
res.send(message); //if i use here message undefined
});你能告诉我在你的控制台上对test值的回应吗?
发布于 2018-07-30 14:40:46
而不是发送为,
res.send(message); //error here你能试着用下面给出的状态码发送同样的邮件吗?
res.status(200).json({
"message":message
})希望这能有所帮助!
发布于 2019-04-01 13:03:32
您忘记了关闭python外壳(在pyshell.on()之后):
pyshell.end(function (err) {
if (err) throw err;
});https://stackoverflow.com/questions/51587602
复制相似问题