I have some code that is repeated among my routes, and I always pass to the view some resulting variables from that repeated code.
Example:
/* GET /events/list */
router.get('/events/list', function(req, res) {
var blah = someFunctionThatAnalyzesUserCookie();
res.render('places/list', { title: 'Places', blah: blah });
});
/* GET /events/details */
router.get('/events/details', function(req, res) {
var blah = someFunctionThatAnalyzesUserCookie();
res.render('places/details', { title: 'Some place', blah: blah });
});
How can I refactor this?
I need to pass blah to all of my views and I want their logics clean, like this:
/* GET /events/list */
router.get('/events/list', function(req, res) {
res.render('places/list', { title: 'Places' });
});
/* GET /events/details */
router.get('/events/details', function(req, res) {
res.render('places/details', { title: 'Some place' });
});
I googled for a while but only found how to execute something in every request, like this:
app.use(function(req, res, next) {
doSomethingInEveryRequest();
next();
});
But I can't figure out how to pass a variable to the view in every request.
Also I read about app.locals, but I guess that is more suited for constants. My passed variable depends on the user's cookies so is a... variable.
path,renderFileName&title?