express 3.0如何使用app.locals.use和res.locals.use [英] express 3.0 how to use app.locals.use and res.locals.use

查看:183
本文介绍了express 3.0如何使用app.locals.use和res.locals.use的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在express3.0rc2。如何使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆