Andrii Rusanov
Andrii Rusanov

Reputation: 4606

Express: pass variable for all templates

I try to pass 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 template, such as

span= token

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

I use Express 3.2

Upvotes: 4

Views: 1397

Answers (1)

matthewtole
matthewtole

Reputation: 3247

The problem is probably to do with where in app.js you put that function. It has to be before the routing middleware, otherwise it won't get called.

Assuming your app.js file has a line like:

app.use(app.router)

Then your function needs to be put before that.

Upvotes: 3

Related Questions