18

I want to log every request with response information.

I tried to use middleware, and I have a problem.

res.body is undefined.

app.use((req, res, next) => {

    var time = Date.now();

    res.on('finish', function() {
        var clientIp = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
        var method = req.method;
        var path = req.baseUrl;
        var duration = Date.now() - time;

        console.log({
            clientIp,
            elapsedTime: `${duration}ms`,
            hostname: req.headers.host,
            level: 'INFO',
            method,
            path,
            phase: process.env.NODE_ENV,
            reqBody: req.body,
            reqHeaders: req.headers,
            resBody: res.body,
            resHeaders: res.getHeaders(),
            status: res.statusCode
        });
    });

    next();
});

Actually there is response data on client.

How can I get response body in middleware?

4
  • Do you want to catch the response of every request on express? Commented Aug 19, 2019 at 7:22
  • @SagarChilukuri yes, Is it possible to catch response on every request?
    – kkangil
    Commented Aug 19, 2019 at 7:26
  • Yes. Adding the solution. Commented Aug 19, 2019 at 7:28
  • Does this answer your question? express logging response body Commented Jan 22, 2022 at 0:31

4 Answers 4

14

The response can be intercepted by overriding response.json function. By doing so, and adding our custom function, every time, response.json() is called, our intercepting function is triggered.

middleware/response.filter.js:

// Response Interceptor Middleware
export default (request, response, next) => {
    try {
        const oldJSON = response.json;
        response.json = (data) => {
            // For Async call, handle the promise and then set the data to `oldJson`
            if (data && data.then != undefined) {
                // Resetting json to original to avoid cyclic call.
                return data.then((responseData) => {
                    // Custom logic/code.
                    response.json = oldJSON;
                    return oldJSON.call(response, responseData);
                }).catch((error) => {
                    next(error);
                });
            } else {
                // For non-async interceptor functions
                // Resetting json to original to avoid cyclic call.
                // Custom logic/code.
                response.json = oldJSON;
                return oldJSON.call(response, finalResponse);
            }
        }
    } catch (error) {
        next(error);
    }
}

In the Server.js file, register the middleware:

// Server.js file
import externalResponseFilter from "./middleware/response.filter.js:";

// Create Express server
const app = express();

// Response interceptor - Initialization.
app.use(externalResponseFilter);

And in the controller where you are returning the response, return with response.json() function instead of response.send().

Let me know if any additional explanation is required.

8
  • Thank you for your answer. I tried your code, but I have a problem that I'm using apollo-server so I couldn't catch res.body. I think overriding response.json function is not running. Do you have any suggestion to solve this problem?
    – kkangil
    Commented Aug 19, 2019 at 8:01
  • I am not sure of apollo-server as it handles the request response communication internally. Commented Aug 19, 2019 at 8:27
  • 1
    thanks anyway. I think I can use your answer other project.
    – kkangil
    Commented Aug 19, 2019 at 8:34
  • 1
    Did you actually test this middleware? I expect that it doesn't work at all, because it doesn't call next() unless there's an error, so it breaks the middleware chain entirely, and my very simple test server doesn't even get to the handler function. And it really shouldn't be calling the outer next() inside the overridden function. Commented Jan 22, 2022 at 0:31
  • 1
    What is the finalResponse object? Commented Oct 13, 2022 at 19:05
14

Intercept the response body from res.json and store it in res.locals for access later.

app.express.use((req, res, next) => {
  const oldJson = res.json;
  res.json = (body) => {
    res.locals.body = body;
    return oldJson.call(res, body);
  };
  next();
});
1
  • What's the difference between this and the best answer? Commented Oct 13, 2022 at 19:00
0

The response object in Express is simply node's http.ServerResponse class. It is a writable Stream so you can interact with it as a stream. It also exposes the underlying net.Socket connection via res.socket.

This is where it gets interesting. Because net.Socket is a plain writable Stream. If you override it's .write() method you will be able to intercept all bytes going out of your socket onto the network:

function interceptorMiddleware (req, res, next) {
    const sock  = req.socket;
    const write = sock.write.bind(sock);

    sock.write = (data, encoding, callback) => {
        console.log(data);
        write(data, encoding, callback);
    }

    sock.on('close', () => console.log('DONE'));

    next();
}

You need to install this middleware very early in your application before anything gets a chance to write to the socket.

Due to the asynchronous nature of node you may get mixed packets in your log output so it may be worth adding a bit of code to identify which socket is generating an output:

console.log(sock.remoteAddress, sock.remotePort, data);

Warning!

console.log() is synchronous when outputting to the terminal or a file. Doing this can potentially severely degrade your server's performance. If you are piping stdout to another process such as pm2 then it's not so bad because it will be async but it may still cost you a minor performance hit.

Use this for debugging ONLY

0

Working perfectly in my project.

log.middleware.js

const uuid = require('uuid').v4; 
const log = require('../logger');

function middleware() {
    return async (req, res, next) => {
        try {
            req.user || (req.user = { trxId: uuid() });
            const { user, url, params, query, body } = req;
            log.debug({ user, url, params, query, reqBody: body }, "request info.");
            const oldJSON = res.json;
            res.json = async (value) => {
                const data = await Promise.resolve(value);
                log.debug({ user, resBody: data }, "response info.");
                return oldJSON.call(res, data);
            }
        } catch (error) {
            log.error(error, "got error");
            return next(error)
        }

        next();
    }
}
module.exports = middleware;

app.js

const logMiddleware = require("./middlewares/log.middleware");
const routes = require('./routes');

const app = express();
app.use(logMiddleware());
app.use("/", routes);
app.listen(8080, () => {
    log.info(`Server started`);
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.