node.js - Express.js unique var per request outside routing -
in express application have module called helpers thats required in routes , modules. module has logger method logs fluentd (but that's unimportant). while building data log i'd add unique identifier of request, logs written same request have same unique id. using global var in app entry point app.use doesn't work because var overwritten every time new request hits, global uuid change change in case of high load or long running tasks. res.locals not available outside routing, can't use matter. there way create var unique per request , available in every module or maybe way access res.locals data outside routing? thank you
edit
maybe example understand better question. suppose have module called helpers.js this:
let helpers = {}; helpers.log = (logdata, logname) => { fluentlogger.emit('', { name: logname, //uuid: needed uuid, message: logdata }); } module.exports = helpers;
now can in app.js entry point:
app.use(function (req, res, next) { res.locals.uuid = uuid.v4(); next(); });
and in every loaded middleware module requires helpers(adding new param helpers.log method):
const helpers = require('helpers'); router.post('/', (req, res, next) => { helpers.log('my log message', 'mylogname', res.locals.uuid); next(); });
and work. suppose big or middle size project there hundreds of custom modules , models (not middlewares) , module may require other modules require other modules require helpers module. in case should pass res.locals.uuid parameter every method of every method have available in logger method. not idea. suppose have new module called dbmodel.js required in middleware function:
const helpers = require('helpers'); let dbmodel = {}; dbmodel.getsomedata = (someparam) => { //some logic helpers.log('my log message', 'mylogname'); } module.exports = dbmodel;
the dbmodel has no idea res.locals data if don't pass middleware, helpers.log method have no idea this.
in php 1 write global var in application's entry point hypothetical logger function have access global on every method request whichever class of application.
hope explanation :) thank you
edit 2
the solution kind of problems cls. @robertklep hint. slideshare explaining same problem (logger unique id) , explaining cls solutions can found here: https://www.slideshare.net/isharabash/cls-and-asynclistener
yes can 1 method .
every request comes routes pass request inside middleware. suppose have
app.get('/', function(req, res) { res.sendfile(path.join(public + "index.html")); });
a request.
place middleware in .and edit req field coming , in way unique variable values each request
check out . https://expressjs.com/en/guide/writing-middleware.html
like this
var requesttime = function (req, res, next) { req.requesttime = date.now() next() } app.use(requesttime) app.get('/', function (req, res) { var responsetext = 'hello world!<br>' responsetext += '<small>requested at: ' + req.requesttime + '</small>' res.send(responsetext) })
here req.requesttime unique each request.
Comments
Post a Comment