Express Middleware to populate the Jade variable for all app.get () applications

I have a jade file that applies to all my templates called layout.jade. In it, I want to have a logout button if the user is currently logged in (this is tracked in req.session).

So layout.jade will have something like

-if (loggedin) a.navButton(href="/logout") Log Out 

The route for the page will look something like this:

 app.get("/foo", function(req, res) { res.render("foo", {loggedin: req.session.isValidUser}); }); 

The thing is, I don’t want to fill in the loggedin variable manually in every single route. Is there a way I can use Express middleware to automatically set some default parameters for an object sent by res.render? Or is there a better way to do this?

Basically, I ask how I can have a set of variables that are always sent to templates, as well as the ability to have specific user variables available in certain templates, manually setting them in routes.

+7
javascript pug express
source share
3 answers

This seems to be in fact a documentary function with which it is simply hard for me to find unless someone has a better way to do this; From the latest Express Documentation ,

app.locals : local application variables are provided for all patterns displayed in the application. This is useful for providing assistant functions for templates as well as application level data.

So, in my function of successful login,

 req.session.username = profile.username; app.locals.username = profile.username; 

My logout function,

 app.get('/logout', function (req, res) { delete app.locals.username; req.session.destroy(); res.redirect('/login'); }); 

And finally in layout.jade / all my templates,

 - if(username) a.navButton(href="/logout") Logout 
+7
source share

If you set res.locals.loggedin to the /login route, as hexacanide suggests, this locale will not be accessible on the route to /foo . res.locals is cleared for each request.

instead, you could put this above other routes:

 app.all('*', function(req, res, next){ if(req.user){ res.locals.loggedin = true; res.locals.currentuser = req.user; }; next(); }); 

Pretty sure that if you change req.user during your route, the res.locals.currentuser that you installed earlier will not be updated to be the new req.user . but not sure about that.

In fact, I use the custom render function for each page where I create the template, it looks like this:

 function myRender(templateName){ return function(req, res){ res.locals.currentuser = req.user || null; res.render(templateName); }; }; 

and I use it as follows:

 app.get('/foo' , function(req, res, next){ res.locals.bar = req.query['bar'] || ""; console.log('passing request to myRender middleware'); next(); } , myRender('foo-jade-template') ) 

This has an advantage only when installing res.locals.currentuser , when I am ready to do something, and not before completing my route. Therefore, if I modify req.user , you will have the latest version at the time of rendering.

+4
source share

There is a line of code that is useful to you in the Express source:

 // merge res.locals options._locals = self.locals; 

Therefore, when you run res.render() , Express will also take any locales that are stored in res.locals and pass them to the render. Therefore, all you have to do is set res.locals.loggedin to true and then run res.render() as usual.

 app.get('/login', function(res, res) { res.locals.loggedin = true; }); app.get('/foo', function(req, res) { res.render('foo', {}); }); 
+1
source share

All Articles