Node Express 4 middleware after routes

2019-04-03 06:49发布

问题:

Following the upgrade to Express 4, and the removal of app.router, I'm struggling to get middleware to execute after routes execute.

e.g. the following code correctly responds with "hello", but never calls the configured middleware

var express = require( "express" )();

express.get( "/", function( req, res ) {

    res.send( "hello" );

} );
express.use( function( req, res, next ) {

    console.log( "world" );
    next();

} );

express.listen( 8888 );

CLARIFICATION:

the following code shows "before" on the console, but not "after":

var express = require( "express" )();

express.use( function( req, res, next ) {

    console.log( "before" );
    next();

} );
express.get( "/", function( req, res ) {

    res.send( "hello" );

} );
express.use( function( req, res, next ) {

    console.log( "after" );
    next();

} );

express.listen( 8888 );

回答1:

In regards to Express 4, the "after" function from your second example never gets called because the middle function never calls next().

If you want the "after" function to get called, then you need to add and call the next callback from your middle function like this:

var express = require( "express" )();

express.use( function( req, res, next ) {

  console.log( "before" );
  next();

} );
express.get( "/", function( req, res, next ) {

  res.send( "hello" );
  next();      // <=== call next for following middleware 

} );
express.use( function( req, res, next ) {

  console.log( "after" );
  next();

} );

express.listen( 8888 );

res.send() writes the headers and response back to the client.

Beware, that once res.send() has been called, you won't want to update your response headers or contents. But you can do other tasks like database updates or logging.

Note that express looks at the the number of arguments in the middleware function and does different logic. Take express error handlers for example, which have 4 parameters defined.

express error handler signature: app.use(function(err, req, res, next) {});

Calling next on the very last item in your middleware chain is optional, but probably a good idea in case you ever change things around.



回答2:

The correct answer is using the res.on("finish", cb) callback.

i.e.:

express.use(function(req, res, next) {
    console.log("before");

    res.on("finish", function() {
        console.log("after");
    });

    next();
});


回答3:

Have you checked putting your console.log after the next() call?

express.use( function( req, res, next ) {
  next();
  console.log( "world" );
});
express.get( "/", function( req, res ) {
  res.send( "hello" );
});


回答4:

Order is important http://expressjs.com/4x/api.html#app.use

express.use( function( req, res, next ) {
  console.log( "world" );
  next();
});
express.get( "/", function( req, res ) {
  res.send( "hello" );
});


回答5:

If you're able to work with another framework, I suggest you to use ZinkyJS, it has a built-in way to do that. Check here: ZinkyJS AFTER HOOKS