Reputation: 711
I don't know why express-messages is not working. I specified it in the app.js, specified it in the router/controller, and the view.
This is part of my app.js:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var exphbs = require('express-handlebars');
var expressValidator = require('express-validator');
var flash = require('connect-flash');
var session = require('express-session');
app.use(flash());
app.use(require('connect-flash')());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
if(req.url == '/'){
res.locals.isHome = true;
}
next();
});
This is my route where I specify the flash message:
router.post('/login', passport.authenticate('local', {failureRedirect: '/', failureFlash: 'Invalid username or password'}), function(req, res){
console.log(req.body);
console.log('Authentication Successful');
req.flash('success', 'You are logged in');
var usertype = req.user.type;
res.redirect('/'+ usertype + 's/classes');
});
router.get('/logout', function(req, res){
req.logout();
req.flash('success', "You have logged out");
res.redirect('/');
})
And this is my handlebar view where I have the messages tag:
<div class="col_12">
<div class="col_9">
{{{messages}}}
{{#if errors}}
{{#each errors}}
<div class="notice error">
<i class="icon-remove-sign icon-large"></i>
{{msg}}
<a href="#close" class="icon-remove"></a>
</div>
{{/each}}
{{/if}}
{{{body}}}
Upvotes: 0
Views: 6025
Reputation: 2873
connect-flash saves the message into the session. You need use express-session before you add the flash
middleware.
app.use(session());
app.use(flash())
Upvotes: 5