【问题标题】:Cannot set headers after they are sent to client将标头发送到客户端后无法设置标头
【发布时间】:2021-06-18 18:58:32
【问题描述】:

我正在创建一个 api,它将在 nodejs 中使用 python-shell 调用 python 脚本。但是,当我运行 api 时,api 返回错误“发送到客户端后无法设置标头”。下面是api的代码。

app.post(
    "/uploadFacial",
    multer({
      storage: storage
    }).single('upload'), function(req, res) {
      console.log(req.file.filename);
      var userid = req.body.upload[1];
      console.log("User id: "+userid);

      var success = true;
      var message = '';

      var subStringUserId = userid.substring(1,userid.length-1);
      var dir = "./users/"+subStringUserId;

      //res.setHeader("Content-Type", "application/json");
      
      console.log(dir);
      if (!fs.existsSync(dir)){
        fs.mkdirSync(dir);
        success = false;
        message = 'Cannot detect the person.Please add name in the textbox provided below to save the person.';
        res.status(200).json({message: message,success:success});
        res.end();
      }else {
        console.log("in else");
        var options={
          mode:'text',
          encoding:'utf-8',
          pythonOptions:['-u'],
          scriptPath:'./',
          pythonPath:'',
          args:[subStringUserId,req.file.filename]
        }
        var facialScript = new PythonShell('face_detect.py',options)
        facialScript.on('message',(response)=>{
          console.log(response);
          res.status(200).send(response);
          //res.end();
        
        })

        facialScript.on('error',(err)=>{
          console.log(err);
        })
      }
    });

如果需要,下面是python代码

from deepface import DeepFace
from os.path import join, dirname, realpath
import os, sys
import json


PARENT_DIR = join(dirname(realpath(__file__)), './users/')
TEST_DIR = join(dirname(realpath(__file__)), './uploads/')

print(json.dumps({'success':False,'message':'Cannot detect at moment.Please try later'})) #this is only for debugging
user_id = sys.argv[1] 


img_name = sys.argv[2]


db = os.path.join(PARENT_DIR, user_id)
img = os.path.join(TEST_DIR, img_name)

try:
    df=DeepFace.find(img_path=img,db_path=db, model_name = "Facenet")

    if(df.size==0):
        print(json.dumps({'message': 'Cannot detect the person.Please add name in the textbox provided below to save the person.','success':False}))
    else:
        print(json.dumps({'success':True,'message':os.path.splitext(os.path.basename(df['identity'][0]))[0]}))        
        
         
except Exception as e: 
    #logger.error('Failed to upload to ftp: '+ str(e))
    a = 1

这是错误

 throw new ERR_HTTP_HEADERS_SENT('set');
    ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:470:11)
    at ServerResponse.header (/Users/priyamvora/Node/Drishti/ImageUploadTest/node_modules/express/lib/response.js:771:10)
    at ServerResponse.send (/Users/priyamvora/Node/Drishti/ImageUploadTest/node_modules/express/lib/response.js:170:12)
    at PythonShell.facialScript.on (/Users/priyamvora/Node/Drishti/ImageUploadTest/multipart.js:129:27)
    at PythonShell.emit (events.js:182:13)
    at /Users/priyamvora/Node/Drishti/ImageUploadTest/node_modules/python-shell/index.js:324:22
    at Array.forEach (<anonymous>)
    at PythonShell.receiveInternal (/Users/priyamvora/Node/Drishti/ImageUploadTest/node_modules/python-shell/index.js:322:15)
    at PythonShell.receive (/Users/priyamvora/Node/Drishti/ImageUploadTest/node_modules/python-shell/index.js:296:21)
    at Socket.emit (events.js:182:13)

请尽早提供帮助。

【问题讨论】:

  • 当您尝试多次发送响应时会出现此错误,
  • @Ashiq 如何解决我的代码问题?我知道问题的原因,但不确定如何解决我的代码问题。
  • 在您的评论中,我看到 res.send() 和 res.end() 在第 20 到 30 行左右使用了一些地方。我在这里读到了 [stackoverflow.com/questions/29555290/… 在 res.send 和 res.end 之间)
  • 不使用 res.end() 仍然无法正常工作。我用它来调试一些东西。

标签: node.js


【解决方案1】:

这是因为“消息”事件侦听器被多次触发,这就是为什么甚至 res.status(200).send(response); 行也多次执行导致此问题的原因。尝试将事件侦听器的结果累积在一个变量中并发送响应(仅一次)。 像这样的:

let result="";
 facialScript.on('message',(response)=>{
         result=result+response;//assuming that response is string you can change this as per your convenience .
        
        })

facialScript.on("end",()=>{
res.send(result);
})

你还必须在 if 条件中删除 res.status(200).json({message: message,success:success}); 来识别目录是否存在!!

【讨论】:

    【解决方案2】:

    如果这个 if 循环被执行,将会发送一个响应

    if (!fs.existsSync(dir)){
            fs.mkdirSync(dir);
            success = false;
            message = 'Cannot detect the person.Please add name in the textbox provided below to save the person.';
            res.status(200).json({message: message,success:success});
            res.end();
    

    响应也在尝试发送以下代码

    var facialScript = new PythonShell('face_detect.py',options)
            facialScript.on('message',(response)=>{
              console.log(response);
              res.status(200).send(response);
              //res.end();
            
            })
    

    不包含在 else 循环中。

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 2021-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 2019-10-20
      相关资源
      最近更新 更多