Crazy Penguin Crazy Penguin - 3 months ago 20
Node.js Question

Node.Js partial data

I want to have some default data accessible in all views in my Node.Js application. My data need to be in /views/layout/header.ejs

└── views
├── index.ejs
└── layout
├── footer.ejs
└── header.ejs

Answer Source

Add the following middleware to your main file.

app.use(function (req, res, next) {
      res.locals.user = req.user  //say you need loggedin user in all your views

In ejs you can use user anywhere, for example:

<%= user.userName %>