终止ExpressJS请求

cyb*_*bat 8 node.js express

我如何结束Express 3请求?res.end()肯定不会停止处理.

exports.home = function(req, res) {
  res.end();
  console.log('Still going, going...');
Run Code Online (Sandbox Code Playgroud)

jma*_*777 11

你需要return.例如,

exports.home = function(req, res) {
  return res.end();
  console.log('I will never run...');
Run Code Online (Sandbox Code Playgroud)

res.end()只需完成并刷新对客户端的响应.然而,就像任何其他操作一样,它不会告诉JavaScript停止运行所以我们需要明确地return退出该函数(尽管我可能会问到为什么在刷新您实际不想要的响应之后会有代码的问题跑步...?).