HDTutorial
EN

Playback starts YouTube’s embedded player (privacy-enhanced mode).

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

18,405 views December 25, 2019 4:40

Watch on YouTube
ChannelTechnical Babaji

Description from the creator

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

About this tutorial

Technical Babaji published this 4:40 video tutorial on December 25, 2019. It has been watched 18,405 times on YouTube.

Category
Education
Duration
4:40
Published
December 25, 2019
Views
18,405
Likes
206

Tags

Share

Further reading

  • Intercept response body in NodeJS express app — CipherTrick ciphertrick.com

    Intercept response body or transform response body in express which, ever way you call it, one thing is for sure its not as straight forward as it is for the request body object. In this tutorial we will write an expres…

  • javascript - Node.js / Express.js - How to override/intercept ... stackoverflow.com

    As you may know javascript is a Prototype-based language and every object has a prototype, like response and request objects. By looking at code (express 4.13.4) you can find their prototypes: req => express.request res…

  • How To Use the res Object in Express | DigitalOcean digitalocean.com

    In this article, you will learn about the res object in Express. Short for response, the res object is one half of the request and response cycle to send data from the server to the client-side through HTTP requests. Pr…

This tutorial in other languages