Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Express.js req or session from Jade template

I am wondering if there is an easy way to access Express.js' req or session variables from within a Jade template without passing it in through the normal response.

Or is this the only way?

res.render('/', {     session: req.session }); 
like image 460
MrBojangles Avatar asked Jun 13 '11 14:06

MrBojangles


People also ask

Is Jade template engines can be used with node js?

Jade, Vash, and Handlebars are template engines that can be used with Node. js.

Where are Express sessions stored?

Where is the session data stored? It depends on how you set up the express-session module. All solutions store the session id in a cookie, and keep the data server-side. The client will receive the session id in a cookie, and will send it along with every HTTP request.

What is Jade in Express JS?

Jade is a template engine for node. js and the default rendering engine for the Express web framework. It is a new, simplified language that compiles into HTML and is extremely useful for web developers. Jade is designed primarily for server-side templating in node.


1 Answers

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} 
like image 130
Ajouve Avatar answered Sep 29 '22 17:09

Ajouve