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

标签 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 only be processed if it has that header.

});

最佳答案

只需使用返回:

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.

});

关于node.js - res.send,之后如何退出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23165569/

相关文章:

node.js - sails 中的 Node js Passport-Facebook 策略身份验证

node.js - 在 Express Web 应用程序中享受全面服务

node.js - 运行 balloons.io 时出错

node.js - mailgun 传入邮件事件获取附件 url

javascript - 将 set-cookie header 发送到 node.js 中的重定向 url

node.js - Push 或 addToSet 不适用于 Express、Node 和 mongoose(获取响应 { ok : 0, n : 0, nModified: 0 })

json - 发布 json 来表达 - 无效的 json

node.js - Gulp watch 因 Spawn EACCES 错误而失败

node.js - package.json 中的依赖是什么 - nodejs

node.js - 从 Typescript 类定义 ExpressJS 路由