ExpressJS 3.0如何将res.locals传递给玉视图? [英] ExpressJS 3.0 How to pass res.locals to a jade view?

查看:324
本文介绍了ExpressJS 3.0如何将res.locals传递给玉视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户登录失败后显示一条Flash消息,但是我无法将变量显示在我的Jade视图中。

I want to display a flash message after a user fails to sign in but I just can't get the variables to show up in my Jade views.

我有一些作品,我知道我必须在我的app.configure()中使用这个:

I have some pieces, I know I have to use this in my app.configure():

    app.use (req, res, next) ->
      res.locals.session = req.session

我会设置什么闪光消息是在用户POSTS错误的密码后:

And I'll set what the flash message is after the user POSTS the wrong password:

     exports.postSession = (req, res) ->
       users = require '../DB/users'
       users.authenticate(req.body.login, req.body.password, (user) ->
       if(user)
         req.session.user = user
         res.redirect(req.body.redirect || '/')
       else
         req.session.flash = 'Authentication Failure!'
         res.render('sessions/new', {title:'New', redirect: req.body.redirect })
      )

我不知道如何在我的Jade文件中访问 res.locals.session 。我怀疑我正在设置一切正确。这个问题很像这样:迁移Express.js 2到3,特别是app.dynamicHelpers()到app.locals.use?,但我仍然无法让它工作。如果有人可以向我显示一个在res.local中设置值并在视图中访问它们的简单示例,那将是非常感激的。

I don't know how to access res.locals.session in my Jade file. I doubt I am setting everything up right. This question is a lot like this one: Migrating Express.js 2 to 3, specifically app.dynamicHelpers() to app.locals.use? but I still can't get it to work. It would be much appreciated if someone could show me just a simple example of setting values in res.local and accessing them in a view.

p.s。我知道连接闪存,但我需要了解如何使视图中的东西可用。

p.s. I do know about connect-flash but I need to understand how to make things available in views.

这是我的应用程序:

app.configure(() -> 
  app.set('views', __dirname + '/views')
  app.set('view engine', 'jade')
  app.use(express.bodyParser())
  app.engine('.jade', require('jade').__express)
  app.use(express.methodOverride())
  app.use(express.cookieParser())
  app.use(express.session({ store: new express.session.MemoryStore({reapInterval: 50000 * 10}), secret: 'chubby bunny' }))
  app.use(express.static(__dirname + '/public'))
  app.use((req, res, next) ->
    res.locals.session = req.session
    next()
  )
  app.use(app.router)
)


推荐答案

只是给每个有同样问题的人简短的总结,得到被解决的印象。 res.redirect

Just to give a short summary for everyone who has the same problem and got the impression that is was solved changing res.redirect.

app.use middleware be前端 app.router 。参见TJ Holowaychuck的评论,作者是

It is very important to put your app.use middleware before app.router. See the comments by TJ Holowaychuck, the author of express

  • https://groups.google.com/d/msg/express-js/72WPl2UKA2Q/dEndrRj6uhgJ

这里是一个使用express v3.0.0rc4的全新安装的示例

Here is an example using a fresh installation of express v3.0.0rc4

app.js:

app.use(function(req, res, next){
  res.locals.variable = "some content";
  next();
})

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

index.jade:

index.jade:

extends layout

block content
  h1= title
  p Welcome to #{title}
  p= variable

这篇关于ExpressJS 3.0如何将res.locals传递给玉视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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