发送响应后,如何结束Node/Express中的当前请求处理? [英] After sending response, how to end the current request processing in Node/Express?

查看:554
本文介绍了发送响应后,如何结束Node/Express中的当前请求处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于此问题的帖子很少,但没有一个可以直接直接回答该问题.让我澄清一下,我理解(或我认为)next(),next('route'),return next(),return及其对控制流的影响的用法. 我用于该应用程序的整个中间件均由一系列app.use组成,如下所示:

There are a few posts on this question but none that answers the issue directly, head-on. Let me clarify that I understand (or so I think) the use of next(), next('route'), return next(), return and their impact on control flow. My entire middleware for the app consists of a series of app.use, as in:

 app.use(f1);
 app.use(f2);
 app.use(f3);
 app.use(f4);
 ...

在这些中间件中的每一个中,我都可以发送响应,并且无需进一步处理即可完成.我的问题是我无法阻止处理过程进入下一个中间件.

In each of these middlewares, I have possibility of sending the response and be done without any need for further processing. My problem is that I am unable to stop the processing from going to the next middleware.

我工作笨拙.我只是在发送响应后设置了res.locals.completed标志.在所有中间件中,一开始我都会检查此标志,如果设置了该标志,则跳过中间件中的处理.在第一个中间件中,未设置此标志.

I have a clumsy work around. I just set a res.locals.completed flag after sending a response. In all the middlewares, at the very start, I check this flag and skip processing in the middleware if the flag is set. In the very first middleware, this flag is unset.

当然,必须有一个更好的解决方案,这是什么?我认为Express会隐式地执行此检查,并通过某些特定于Express的方法来跳过中间件?

Surely, there must be a better solution, what is it? I would think that Express implicitly would do this checking and skip the middlewares through some express-specific method?

推荐答案

根据 http://expressjs.com/guide/using-middleware.html

If the current middleware does not end the request-response cycle,
it must call next() to pass control to the next middleware,
otherwise the request will be left hanging.

因此,如果中间件需要尽早结束请求-响应,只需不调用next(),而是通过调用res.endres.sendres.render或任何其他方法确保中间件确实结束了请求-响应隐式调用res.end

so if a middleware needs to end the request-response early, simply do not call next() but make sure that the middleware really ends the request-response by calling res.end, res.send, res.render or any method that implicitely calls res.end

app.use(function (req, res, next) {
  if (/* stop here */) {
    res.end();
  } else {
    next();
  }
});

这是一个显示其正常工作的示例服务器

Here is an example server showing that it works

var express = require('express');
var app = express();

var count = 0;
app.use(function(req, res, next) { 
  console.log('f1'); 
  next();
 })
app.use(function(req, res, next) {
  console.log('f2');
  if (count > 1) {
    res.send('Bye');
  } else {
    next();
  }
})
app.use(function(req, res, next) {
  console.log('f3');
  count++;
  next();
})

app.get('/', function (req, res) {
  res.send('Hello World: ' + count);
});

var server = app.listen(3000);

您将看到以下3个请求,服务器显示再见"并且未达到f3

you will see the after 3 requests, the server shows "Bye" and f3 is not reached

这篇关于发送响应后,如何结束Node/Express中的当前请求处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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