2012-11-07 69 views
0

即時嘗試讓連接閃光燈在我的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頁面上的說明。我錯過了什麼?

回答

1

反向排序:

app.use(flash()); 

app.use(function(req, res, next) { 
    res.locals.message = req.flash(); 
    next(); 
});