如何在调用 Responde.end() 后执行 NodeJS/connect 中间件? [英] How to have a NodeJS/connect middleware execute after responde.end() has been invoked?

查看:24
本文介绍了如何在调用 Responde.end() 后执行 NodeJS/connect 中间件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现这样的目标:

var c = require('connect');
var app = c();

app.use("/api", function(req, res, next){
    console.log("request filter 1");
    next();
});

app.use("/api", function(req, res, next){
    console.log("request filter 2");
    next();
});

app.use("/api", function(req, res, next){
   console.log("request handler");
    res.end("hello");
    next();
});

app.use("/api", function(req, res, next){
    console.log("response post processor");
    next();
});
app.listen(3000);

当我为地址卷曲时,控制台出现异常,抱怨头在发送后无法被打扰,这很公平.只是我没有接触响应对象.

When I curl for the address, I get an exception to the console complaining about headers cannot be bothered after being sent which is fair enough. Only that I do not touch the response object.

/usr/bin/node app2.js
request filter 1
request filter 2
request handler
Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (http.js:644:11)
    at ServerResponse.res.setHeader (/home/zpace/node_modules/connect/lib/patch.js:59:22)
    at next (/home/zpace/node_modules/connect/lib/proto.js:153:13)
    at Object.handle (/home/zpace/WebstormProjects/untitled1/app2.js:25:5)
    at next (/home/zpace/node_modules/connect/lib/proto.js:190:15)
    at Object.handle (/home/zpace/WebstormProjects/untitled1/app2.js:19:5)
    at next (/home/zpace/node_modules/connect/lib/proto.js:190:15)
    at Object.handle (/home/zpace/WebstormProjects/untitled1/app2.js:14:5)
    at next (/home/zpace/node_modules/connect/lib/proto.js:190:15)
    at Function.app.handle (/home/zpace/node_modules/connect/lib/proto.js:198:3)

调试 NodeJS/Connect 层我进入了一个部分,它以某种方式暗示如果头已经发送,那么执行路由处理程序必须初始化响应头.

Debugging the NodeJS/Connect layer I got into a part that somehow implies that if headers are already sent then executing a route handler must initialize response headers.

问题是上述行为是否是故意的(即在路由处理程序完成发送响应后执行任何代码是完全无法想象的,或者这只是连接中的错误?

The question is if the above mentioned behavior is intentional (ie that the execution of any code after a route handler has finished sending a response is something utterly unimaginable or this is simply a bug in connect?

推荐答案

不确定您是否找到了解决方案.

Not sure whether you have found your solution.

如果你想为请求周期设计一个后处理器,你可以使用一个中间件来监听响应对象上的完成"事件.像这样:

If you want to design a post-processor for the request cycle, you can use a middleware that listens to the "finish" event on the response object. Like this:

app.use(function(req, res, next){
  res.on('finish', function(){
    console.log("Finished " + res.headersSent); // for example
    console.log("Finished " + res.statusCode);  // for example
    // Do whatever you want
  });
  next();
});

finish"事件附带的函数将在响应写出后执行(这意味着 NodeJS 已经将响应头和主体交给操作系统进行网络传输).

The function attached to the "finish" event will be executed after the response is written out (which means the NodeJS has handed off the response header and body to the OS for network transmission).

我想这一定是你想要的.

I guess this must be what you want.

这篇关于如何在调用 Responde.end() 后执行 NodeJS/connect 中间件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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