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

Опубликовано: 26 Март 2026
на канале: Technical Babaji (Tarique Akhtar)
18,368
207

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