Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify request (by ID) through middleware chain in Express.

I am developping a RESTful server in node.js, using Express as framework, and Winston, for the moment, as logger module. This server will handle a big amount of simultaneous request, and it would be very useful to me to be able to track the log entries for each specific request, using something like a 'request ID'. The straight solution is just to add this ID as another piece of logging information each time I want to make a log entry, but it will mean to pass the 'request ID' to each method used by the server.

I would like to know if there is any node.js/javascript module or technique that would allow me to do this in an easier way, without carrying around the request ID for each specific request.

like image 743
Víctor Iniesta Avatar asked Jul 17 '13 13:07

Víctor Iniesta


People also ask

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.

What does the Express JSON () middleware do and when would you need it?

json() is a built-in middleware function in Express. This method is used to parse the incoming requests with JSON payloads and is based upon the bodyparser. This method returns the middleware that only parses JSON and only looks at the requests where the content-type header matches the type option.

How many middleware can be on a single route?

We can use more than one middleware on an Express app instance, which means that we can use more than one middleware inside app.

What is middleware in Express js What are the different types of middleware?

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application's request-response cycle. These functions are used to modify req and res objects for tasks like parsing request bodies, adding response headers, etc.


2 Answers

I was struggling search for a solution for this problem. The thing I didn't like it about solutions suggested here was that they imply to share the req object among all the functions along the project. I found out a solution mixing your approach (creating an uuid per request) and with a library (continuation-local-storage) that allows sharing namespaces among modules.

You can find the explanation in this other answer: https://stackoverflow.com/a/47261545/5710581

If you want more info, I wrote down all these ideas and all the code in a post, in order to explain everything in one place: Express.js: Logging info with global unique request ID – Node.js

like image 75
David Vicente Avatar answered Oct 09 '22 12:10

David Vicente


If you auto-increment, your later log analytics won't be able to uniquely identify requests, because different instances will generate colliding IDs, and restarting the app will automatically cause ID collisions.

Here's another possible solution.

Install cuid:

npm install --save cuid

Then in your main app file:

var cuid = require('cuid');
var requestId = function requestId(req, res, next) {
  req.requestId = cuid();
  next();
};

// Then, at the top of your middleware:
app.use(requestId);

Now you'll get a friendly request ID that is unlikely to collide, and you'll be able to uniquely identify your requests for your log analytics and debugging, even across multiple instances, and server restarts.

like image 40
Eric Elliott Avatar answered Oct 09 '22 12:10

Eric Elliott