How to Intercept Request and Response object in Express js | Nodejs Tutorial

In this video you are going to learn how to intercept request and response object in express js using a single middleware function and log each request and response into a file

middleware code
—————————
app.use(function(req, res, next) {

logger.info(req.body);

let oldSend = res.send;

res.send = function (data) {

logger.info(JSON.parse(data));

oldSend.apply(res, arguments);

}

next();

})

apply the above middleware function before calling any route.
I use winston logger to log request and response object into a file

must watch my previous video on how to log using winston

Source: https://www.youtube.com/watch?v=1jhdfS1Bwcc

Leave a Reply

Your email address will not be published. Required fields are marked *