express/connect middleware which executes after the response is sent to the client

后端 未结 2 1358
日久生厌
日久生厌 2021-02-20 11:44

Is it possible to write a middleware which executes after the response is sent to a client or after the request is processed and called just before sending the response to clien

相关标签:
2条回答
  • 2021-02-20 12:19

    pauljz gave the basic method but to expand on that here is an example of middleware

    module.exports = function() {
      return function(req, res, next) {
        req.on("end", function() {
          // some code to be executed after another middleware
          // does some stuff
        });
        next(); // move onto next middleware
      }
    }
    

    In your main app

    expressApp.use(require("./doneMiddleware"));
    expressApp.use(express.logger());
    expressApp.use(express.static.....
    
    0 讨论(0)
  • 2021-02-20 12:42

    See if binding to req.on('end', function() {...}); will work for you.

    0 讨论(0)
提交回复
热议问题