Express: pass variable for all templates

I am trying to pass a variable to all templates. In app.js, I wrote:

app.use(function(req, res, next){ res.locals.token = '1234'; next(); }); 

But if I try to get my variable in the template, for example

 span= token 

I get an error = 'token is not defined'. What's wrong?

I am using Express 3.2

+4
source share
1 answer

The problem is probably related to where in app.js you put this function. It must be up to the middleware routing, otherwise it will not be called.

Assuming your app.js file has the following line:

 app.use(app.router) 

Then this should be your function.

+3
source

All Articles