express 3.0如何使用app.locals.use和res.locals.use [英] express 3.0 how to use app.locals.use and res.locals.use
问题描述
app.locals.use
(它是否仍然存在)和 res.locals.use
我看到这个 https://github.com/visionmedia / express / issues / 1131 ,但app.locals.use引发错误。我假设一旦把函数放在app.locals.use中,我可以在路由中使用它。
我在想添加
app.locals.use(myMiddleware(req,res,next){res.locals.uname ='fresh'; next();})
然后在任何路由中调用此中间件
谢谢
我使用的是Express 3.0,这对我有用:
app.use(function(req,res,next){
res.locals.myVar ='myVal';
res.locals.myOtherVar =' myOtherVal';
next();
});
然后我可以访问 myVal
和 myOtherVal
在我的模板中(或直接通过 res.locals
)。
I am on express3.0rc2. How to use app.locals.use
(does it still exist) and res.locals.use
I saw this https://github.com/visionmedia/express/issues/1131 but app.locals.use throws an error. I am assuming that once I put the function in app.locals.use I can use it in the routes.
I was thinking of adding
app.locals.use(myMiddleware(req,res,next){res.locals.uname = 'fresh'; next();})
and then in any route call this middleware
thanks
I'm using Express 3.0 and this works for me:
app.use(function(req, res, next) {
res.locals.myVar = 'myVal';
res.locals.myOtherVar = 'myOtherVal';
next();
});
I then have access to myVal
and myOtherVal
in my templates (or directly via res.locals
).
这篇关于express 3.0如何使用app.locals.use和res.locals.use的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!