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

查看:23
本文介绍了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

我会在用户发布错误密码后设置闪现信息:

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. 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 中间件放在 app.router 之前非常重要.查看express作者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

这是一个使用 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天全站免登陆