21

ExpressJS middleware req, res, next have hooks like .on and .pipe.

But I'm looking for hooks for the app.get and app.post methods.

robertklep
  • 198,204
  • 35
  • 394
  • 381
phani
  • 1,134
  • 1
  • 11
  • 24

1 Answers1

55

app.use() and middleware can be used for "before" and a combination of the 'close' and 'finish' events can be used for "after."

app.use(function (req, res, next) {
    function afterResponse() {
        res.removeListener('finish', afterResponse);
        res.removeListener('close', afterResponse);

        // action after response
    }

    res.on('finish', afterResponse);
    res.on('close', afterResponse);

    // action before request
    // eventually calling `next()`
});

app.use(app.router);

An example of this is the logger middleware, which will append to the log after the response by default.

Just make sure this "middleware" is used before app.router as order does matter.

ErikE
  • 48,881
  • 23
  • 151
  • 196
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • It's also interesting, that `logger` listens to both `close` and `finish`. Supposedly that can be useful in some situations. – vkurchatkin Nov 24 '13 at 18:19
  • @vkurchatkin Using both can help with backwards compatibility as `'finish'` is new with Node 0.10 and [Streams2](http://blog.nodejs.org/2012/12/20/streams2/). So, Express is using [`'close'`](http://nodejs.org/docs/v0.8.26/api/stream.html#stream_event_close_1) as well to support lower versions of Node. – Jonathan Lonowski Nov 24 '13 at 18:24
  • Hmm, don't think so: http://nodejs.org/api/http.html#http_event_close_1, http://nodejs.org/api/stream.html#stream_event_finish. Looks like `close` is emitted if connection was terminated berfore `res.end()` call and `finish` is emitted after `res.send()` – vkurchatkin Nov 25 '13 at 12:36
  • @vkurchatkin A bit of both, actually. Note that I wasn't linking to the latest version of the docs as the `'finish'` event didn't exist in a stable release before `v0.10.0`. Before that, both scenarios would emit a `'close'` event -- [`http.ServerResponse` (v0.8.26)](http://nodejs.org/docs/v0.8.26/api/http.html#http_event_close_1) and [Writable Stream (v0.8.26)](http://nodejs.org/docs/v0.8.26/api/stream.html#stream_event_close_1). But, you are right that `'close'` is more than just for backwards support and I've edited for it. – Jonathan Lonowski Nov 25 '13 at 16:08
  • I used this code, and put it right after var app = express(). I used Crockford's decycle to write the request object to the console log. Some of the stuff I know is in the request object doesn't appear. I can change the request, and get a different response from the server, so I know the information is going through, but I can't see it. Is Express, by itself before adding any filters, altering the object? Rick – user2171796 Sep 30 '16 at 14:32
  • 4
    Is it necessary to remove the listener? When the response is finish, neither itself nor the listener will be used again, or will they? –  Oct 22 '16 at 14:26
  • @user659025 : Not sure if this was the OP's reason, but it is worth noting that different versions of Node work differently with regard to the response's `close` and `finish` events... Some versions will send the `finish` event and follow it with the `close` event, while the most recent version (at the moment 10.9) will either send `finish` *or* `close` (depending on whether the response is sent), but not both. Removing the listeners (in `afterResponse`) makes the code work correctly for all versions of node. – logidelic Aug 28 '18 at 13:30