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

旧城冷巷雨未停 提交于 2021-02-07 05:39:18

问题


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 client?


回答1:


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.....



回答2:


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



来源:https://stackoverflow.com/questions/17761814/express-connect-middleware-which-executes-after-the-response-is-sent-to-the-clie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!