javascript - Can't get connect-flash to work with express.js on redirect -
ok reason session flash works when res.render()
, when seek set session flash , redirect, doesn't display. problem below in contact method in else
clause)...
this logged console during redirect:
{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} { info: [ 'thanks homecoming message soon' ] } {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
as can see, message in there, looks it's getting replaced empty object.
here code:
app.js
var flash = require('connect-flash'); // grab sessions var sessionflash = function(req, res, next) { res.locals.messages = req.flash(); console.log(res.locals.messages); next(); } app.configure(function() { app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(viewhelpers()); app.use(express.bodyparser({ uploaddir : './' })); app.use(expressvalidator); app.use(express.methodoverride()); app.use(express.cookieparser('keyboard cat')); app.use(express.session({ secret: config.secret, cookie: { maxage: 365 * 24 * 60 * 60 * 1000 }, store: new mongostore(config.db) })); app.use(flash()); app.use(sessionflash); app.use(express.static(__dirname + '/public')); app.use(app.router); });
views/layout.jade
- if (typeof message !== 'undefined') =message
controllers/index.js
function controllers(params) { controllers.contact = function(req, res) { if (errors) { // works intended req.flash('info', 'please prepare errors below.'); res.render('contact', { title : 'contact -', message: req.flash('info'), errors: errors, params: params }); return; } else { // doesn't work req.flash('info', 'thanks homecoming message soon'); res.redirect('/'); } }; homecoming controllers } module.exports = controllers;
well, redirecting '/', server not respond to.
you don't have app.get('/', function(req, res){...});
in app.js
flash messages messages stored on server duration between 2 requests same client, , available in next request. flash not show because res.redirect('/');
nothing.
javascript node.js express
No comments:
Post a Comment