中间件示例
var app = require('express')()
, jade = require('jade')
app.set('views', __dirname + '/views')
app.set('view engine', 'jade')
app.use(function(req,res,next){
res.locals.user = { name : "test" }
next()
})
app.get('*',function(req,res){
res.render('index.jade')
})
app.listen('8001')
索引.jade
!!! 5
html
body
div hello #{user.name}
使用 req.flash 尝试:
var app = require('express')()
, jade = require('jade')
app.set('views', __dirname + '/views')
app.set('view engine', 'jade')
app.use(require('connect-flash')())
// Expose the flash function to the view layer
app.use(function(req, res, next) {
res.locals.flash = req.flash.bind(req)
next()
})
app.get('*',function(req,res){
res.render('index.jade')
})
app.listen('8001')
在查看选项下的https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x更新了我的答案,尚未迁移到 3.0 :
The "view options" setting is no longer necessary, app.locals are the local variables
merged with res.render()'s, so app.locals.pretty = true is the same as passing
res.render(view, { pretty: true }).