看【一起学node.js】遇到的关于flash的问题:如何把flash写入locals,以便各个页面都能接收flash
##app.js
app.use(function(req, res, next){
//这里为什么不对???
res.locals.user = req.session.user;
res.locals.error = req.flash('error').toString()
res.locals.success = req.flash('success').toString();
next();
});
routes(app);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
##header.ejs
<article>
<% if (locals.success) { %>
<div><%= locals.success %></div>
<% } %>
<% if (locals.error) { %>
<div><%= locals.error %></div>
<% } %>
##index.js
module.exports = function(app){
app.get('/', function(req, res){
res.render('index', {
title: '主页'//,
//设置在locals里就不用每个页面下面都写这些了
//user: req.session.user,
//success: req.flash('success').toString(),
//error: req.flash('error').toString()
});
});
app.get('/reg', function(req, res){
res.render('reg', {
title: '注册',
//user: req.session.user,
//success: req.flash('success').toString(),
//error: req.flash('error').toString()
})
});
......
这样的话,redirect之后,error信息并不会显示出来。这是为什么???
大家有什么见解,或者这个写法现在不适用了,需要替换??