I'm using python shell to run python on nodeJS. I need to return python shell out put as the response. but when i try to send the response it shows an error
Can't set headers after they are sent.
here is my code
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
});
How to slove this?
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
});
Can you tell me the response on your console for the value of test ?
Instead of sending it as,
res.send(message); //error here
Can you please try sending the same with a status code given below?
res.status(200).json({
"message":message
})
Hope this helps!
You forgot to close the python shell (after pyshell.on()):
pyshell.end(function (err) {
if (err) throw err;
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.