簡體   English   中英

從python-shell返回值作為響應

[英]return value from python-shell as response

我正在使用python shell在nodeJS上運行python。 我需要返回python shell作為響應。 但是當我嘗試發送響應時,它顯示了一個錯誤

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


});

如何解決這個問題?

    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值的響應嗎?

與其發送為

res.send(message); //error here

您可以嘗試使用下面給出的狀態碼發送郵件嗎?

res.status(200).json({
  "message":message
})

希望這可以幫助!

您忘記了關閉python shell(在pyshell.on()之后):

pyshell.end(function (err) {
  if (err) throw err;
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM