Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging requests and responses in express middleware

I'm trying to implement a logger in an Express application. I need it to be able to log requests and the response (status code and body) sent back for each request. I started writing a middleware that looks like this:

function (req, res, next) {
    ...
    res.on('finish', function () {
       Logger.debug('For request', req);
       Logger.debug('Response sent');
    });
    ...
}

I need to access the data passed to the res object method used to send the response. For example, if in one controller I had:

res.json({ foo: 'bar' })

I need a way to get that { foo: 'bar' } object, something like this maybe:

function (req, res, next) {
    ...
    res.on('finish', function () {
       Logger.debug('For request', req);
       var data = res.data; // or res.body, or whatever
       Logger.debug('Response: ' + res.statusCode, data);
    });
    ...
}

Is there any property or method in the Express res object that I could use for that? Or, is there a better strategy for logging requests and the responses for them?

like image 769
Maikel Ruiz Avatar asked Jun 01 '16 21:06

Maikel Ruiz


People also ask

Can we use Express middleware for response?

Express middleware refers to a set of functions that execute during the processing of HTTP requests received by an Express application. Middleware functions access the HTTP request and response objects. They either terminate the HTTP request or forward it for further processing to another middleware function.

What is middleware logging?

HTTP Logging is a middleware that logs information about incoming HTTP requests and HTTP responses. HTTP logging provides logs of: HTTP request information. Common properties. Headers.

What are request and response objects in Express?

Express. js Request and Response objects are the parameters of the callback function which is used in Express applications. The express. js request object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on.

How does Middlewares work in Express?

Middleware functions are functions that have access to the request object ( req ), the response object ( res ), and the next function in the application's request-response cycle. The next function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware.


2 Answers

You need an event listener that will be triggered when response.end (or send) methods are called. Express doesn't have such listener but node.js raw api does have.

There is a finish event in node.js http server instances which makes you able to do things after you sent your response to the client.

You can easily integrate with your existing Express server:

response.on('finish', () => {
  // Do your logging here.
});

I recommend that save your logs in a javascript object during request and send them inside of finish event's callback when response has sent.

like image 156
muratgozel Avatar answered Oct 14 '22 02:10

muratgozel


I found an existing module, morgan-body that accomplishes what you're trying to do.

Here's how you'd use the middleware, per the documentation:

const morganBody = require('morgan-body');
const bodyParser = require('body-parser');
const express = require("express");

const app = express();

// must parse body before morganBody as body will be logged
app.use(bodyParser.json());

// hook morganBody to express app (Unlike typical express middleware you're passing the actual app into the function)
morganBody(app);

This answer could be useful if you want to use an out of the box solution or are looking for some reference points on how to implement it yourself.

like image 22
Jakub Kukul Avatar answered Oct 14 '22 02:10

Jakub Kukul