node.js - How to identify request (by ID) through middleware chain in Express. -
i developping restful server in node.js, using express framework, , winston, moment, logger module. server handle big amount of simultaneous request, , useful me able track log entries each specific request, using 'request id'. straight solution add id piece of logging information each time want make log entry, mean pass 'request id' each method used server.
i know if there node.js/javascript module or technique allow me in easier way, without carrying around request id each specific request.
you can use req
object comes every request in express.
first route in application be:
var logiditerator = 0; app.all('*', function(req, res, next) { req.log = { id: ++logiditerator } return next(); });
and anywhere within express, can access id
in req
object: req.log.id
;
still need pass data functions want create logs. in fact might have logging function within req.log
object, way guaranteed logging happen when there access req.log
object.
Comments
Post a Comment