res.send,之后如何退出? [英] res.send, how to exit after?

查看:307
本文介绍了res.send,之后如何退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用res.send()之后,是否需要调用return或以某种方式退出回调函数,以确保没有其他代码执行?就像在PHP中调用标头函数时一样,您需要在此之后调用exit,以防止进一步的代码被执行.

After calling res.send(), is there a need to call a return or somehow exit the callback function in order to make sure no further code executes? Like how when calling a header function in PHP, you need to call exit after that to prevent further code from being executed.

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.

});

推荐答案

只需使用return:

Just use return:

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,之后如何退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆