access session data from ejs view

Solutions on MaxInterview for access session data from ejs view by the best coders in the world

showing results for - "access session data from ejs view"
Daniel
14 Oct 2020
1app.use(function(req, res, next) {
2  res.locals.user = req.session.user;
3  next();
4});
5
6/*
7
8
9You can use res.locals to expose particular data to all templates.
10
11In your situation, you could add the following middleware to Express:
12
13
14This will make a user variable available in all your templates.
15
16You do need to make sure that you add that middleware after req.session has been set (which is usually after express-session has been added to the middleware chain).
17*/