我试图让连接闪存在我的 express3 应用程序中工作。
我成功安装了包:
$ npm install connect-flash
我包括它:
var flash = require('connect-flash');
设置中间件:
app.use(function(req, res, next) {
res.locals.message = req.flash();
next();
});
app.use(flash());
并使用它:
app.get('/admin', function(req, res) {
if(loggedIn === true) {
res.redirect('/admin/books');
}
else {
res.render('login', {message: req.flash('error') });
}
});
app.post('/admin', function(req, res) {
if((adminAccount.username === getCrypted(req.body.username)) &&
(adminAccount.password === getCrypted(req.body.password))) {
loggedIn = true;
res.redirect('/admin/books');
}
else {
req.flash('error', 'Woops, looks like that username and password are incorrect.');
res.redirect('/admin');
}
});
但是我得到:TypeError: Object #<IncomingMessage> has no method 'flash'
。我按照其 github 页面上的说明进行操作。我错过了什么?