Accessing Express.js req or session from Jade template
In express 3.x, dynamicHelpers have been removed so you will need to use a combination of middleware and res.locals
. Let's say we want to access req.query
in a /signup/new
view:
localQuery = function(req, res, next) {
res.locals.query = req.query;
next();
};
newSignup = function(req, res) {
res.render('signup/new');
};
app.get('signup/new', localQuery, newSignup);
Now any route which uses the localQuery
middleware, will have res.locals.query
set. This can then be accessed in your view as query
.
Just add
app.use(express.cookieParser());
app.use(express.session({secret: '1234567890QWERTY'}));
app.use(function(req,res,next){
res.locals.session = req.session;
next();
});
Before
app.use(app.router);
and get your session in jade
p #{session}