Node.js res.send,之后如何退出?

Node.js res.send,之后如何退出?,node.js,express,Node.js,Express,调用res.send()后,是否需要调用return或以某种方式退出回调函数,以确保不再执行任何代码?就像在PHP中调用header函数时,需要在调用之后调用exit,以防止执行进一步的代码 app.post('/create', function(req, res) { if(req.headers['x-api-key'] === undefined) { res.send({msg: "Goodbye"}); } // other code that should o

调用
res.send()
后,是否需要调用return或以某种方式退出回调函数,以确保不再执行任何代码?就像在PHP中调用header函数时,需要在调用之后调用exit,以防止执行进一步的代码

app.post('/create', function(req, res) {
  if(req.headers['x-api-key'] === undefined) {
     res.send({msg: "Goodbye"});
  }
  // other code that should only be processed if it has that header.

});

根据节点手册:

必须对每个响应调用response.end()方法

使用always next()

只需使用返回:

app.post('/create', function(req, res) {
  if(req.headers['x-api-key'] === undefined)
    return res.send({msg: "Goodbye"});

  // other code that should only be processed if it has that header.

});

可能是
res.send()
而不是
req.send()
。如果只是发送响应,调用next()没有意义。@msdex是正确的。请忽略我的回答,因为它没有什么帮助。
app.post('/create', function(req, res) {
  if(req.headers['x-api-key'] === undefined)
    return res.send({msg: "Goodbye"});

  // other code that should only be processed if it has that header.

});