Reputation: 91
I am trying to send data with redirect as:
this.redirect("/home",{message:'hello'});
but I got the result like:
undefined redirect to home
If anyone faced this problem, please help.
Using framework: locomotive
Upvotes: 1
Views: 8511
Reputation: 681
You can, but do not need to, use the express-flash module for flash messaging. The req.session and res.locals objects exposed by the express-session module provide an avenue for writing your own flash middleware to more precisely meet your needs. This one is adapted from Ethan Brown's book, Web Development with Node & Express.
app.use(function(req, res, next){
// if there's a flash message in the session request, make it available in the response
res.locals.flash = req.session.flash;
// then delete it
delete req.session.flash;
next();
});
Set the flash message using req.session.flash before redirecting to the target route.
req.session.sessionFlash = {
type: 'success',
message: 'This is a flash message using custom middleware and express-session.'
}
Retrieve the flash message using res.locals.flash in the target route's res.render method.
res.render('index', { flash: res.locals.flash });
See my Gist below for more details about flash messaging in Express 4.
https://gist.github.com/brianmacarthur/a4e3e0093d368aa8e423
Upvotes: 0
Reputation: 9958
Yes, you can use express-flash:
app.use(flash());
...
app.get('/redirect', function (req, res) {
req.flash('info', 'Flash Message Added');
res.redirect('/');
});
You data are acessile in res.locals.messages
so in your views just in messages
var.
express flash is based on connect flash. It uses sessions to transmit messages.
If you are using locomotive
Locomotive builds on Express, preserving the power and simplicity you've come to expect from Node.
so:
module.exports = function() {
...
this.use(express.bodyParser());
this.use(express.session({ secret: 'keyboard cat' }));
this.use(flash());
...
}
Upvotes: 3