How to have a NodeJS/connect middleware execute after responde.end() has been invoked? How to have a NodeJS/connect middleware execute after responde.end() has been invoked? node.js node.js

How to have a NodeJS/connect middleware execute after responde.end() has been invoked?


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();});

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.


I think this is a bad planning problem. You should solve this in a better way. I dont know why you have a request handler and a request post processor separated, but lets find out what we can do.

So yes, after response has ended you cant read the headers again.

So dont finish the response until the post processor is invoked.

var isEnd;app.use("/*", function(req, res, next){  isEnd = false;})app.use("/api", function(req, res, next){   console.log("request handler");    res.write("hello");    isEnd = true;    next();});app.use("/api", function(req, res, next){    console.log("response post processor");    if(isEnd) {        res.end();    }    else next();});

This is a kind of solution, but this may not be the best for your problem.

In my opinion it is really bad that you call next() after the response has been finished. If you need a post processor, why you do that in a request filterer (or what is this). Call a function but not next()

Maybe this:

app.use("/api", function(req, res, next){   console.log("request handler");    res.end("hello");    setTimeout(function(){(postProcessor(req)},0);});function postProcessor(req) {//doing post process stuff.//response not needed because already ended.}

Or this:

app.use("/api", function(req, res, next){   console.log("request handler");    res.writed("hello");    setTimeout(function(){(postProcessor(req)},0);    // u cant res.end here because setTimeout.     //If you dont use setTimeout you can use res.end here, but not bot function.});function postProcessor(req, res) {//doing post process stuff.res.end();}

The next() is not for that usage, what you uses.

I hope my answer helps you, but i know it not covers everything, but your answer is not really concrete too.


What a great question to try work out with your morning coffee!

So looking through proto.js, if you have a look down to line 102 which is app.handle which is the handler code for the middleware stack, you'll see how next() operates.

Where the function next() is called, you can see it checks if res.headerSent is true and if so it throws an error.

If modify line 14 to:

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

You will see that it actually sets "headersSent" to true. So after we've ended the request, you can see from the next() code that it throws the error because of the conditions discussed.