如何将dynamicvariables传递给所有nodejs视图?
我有路线:
app.all("*", function(req, res){ //i have here some code for example var number = 5; //real code is doing some selected on rethinkdb })
如何在任何视图上访问数字5?
这是行不通的:
res.locales.number = 5 req.session.number = 5
还有什么我可以尝试?
你有它几乎正确,但你拼错了locals
variables,这就是为什么它不工作。
下面是一个示例中间件,它将设置一些variables,这些variables可用于所有用express呈现的模板:
app.use(function(req, res, next) { res.locals.blah = 'something'; next(); })); // Now, from this point on, any template can access the `blah` variable // directly =)