Skip to content

Instantly share code, notes, and snippets.

@raddeus raddeus/app.js
Last active Jan 13, 2020

Embed
What would you like to do?
Basic Express 4.0 Setup with connect-flash
var express = require('express');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var flash = require('connect-flash');
var app = express();
app.use(cookieParser('secret'));
app.use(session({cookie: { maxAge: 60000 }}));
app.use(flash());
app.all('/', function(req, res){
req.flash('test', 'it worked');
res.redirect('/test')
});
app.all('/test', function(req, res){
res.send(JSON.stringify(req.flash('test')));
});
app.listen(3000);
module.exports = app;
{
"dependencies": {
"express": "^4.0.0",
"express-session": "^1.0.2",
"cookie-parser": "^1.0.1",
"connect-flash": "^0.1.1"
}
}
@immuzov

This comment has been minimized.

Copy link

immuzov commented Jul 11, 2014

Thank you, this example helped me.

@jmcbee

This comment has been minimized.

Copy link

jmcbee commented Sep 29, 2014

excellent.

@sespinosa

This comment has been minimized.

Copy link

sespinosa commented Dec 13, 2014

ty sir

@IvanMMM

This comment has been minimized.

Copy link

IvanMMM commented Apr 13, 2015

Thanks :)

@Twanawebtech

This comment has been minimized.

Copy link

Twanawebtech commented May 16, 2016

awesome dude thanks

@colepacak

This comment has been minimized.

Copy link

colepacak commented Jun 2, 2016

So helpful. Thank you.

@ghost

This comment has been minimized.

Copy link

ghost commented Dec 2, 2016

beautiful.

@Adizbek

This comment has been minimized.

Copy link

Adizbek commented May 6, 2017

Thanks.

@rshamsy

This comment has been minimized.

Copy link

rshamsy commented Sep 24, 2018

Thank you. Why does req.flash(messageName_string) return an array?

@sayeed-ahmed-rasel

This comment has been minimized.

Copy link

sayeed-ahmed-rasel commented Mar 5, 2019

thank you very much ! i found solution after 1 hr searching with my problem.

@tezeoffor

This comment has been minimized.

Copy link

tezeoffor commented Jul 3, 2019

When I pass the message to an ejs file it returns an empty array. Whats the problem?

@MJB-Khorasani

This comment has been minimized.

Copy link

MJB-Khorasani commented Sep 16, 2019

hi @raddeus. why i must use connect-flash, when i can send message with res.render()?
res.render('path/to/view', { message: 'this is a warning message' })

@raddeus

This comment has been minimized.

Copy link
Owner Author

raddeus commented Sep 16, 2019

@MJB-Khorasani - Flash messaging is useful when you want to show a message on a subsequent request. For example, a user tries to delete a resource, but does not have permission. Rather than showing the resources view from the delete route, you would redirect the user back to the resource view with a message stating that they do not have permission. To do this you have to store that message in the session and clear that session when the message is displayed (often on every request, with a classic server-rendered application).

@snakehead007

This comment has been minimized.

Copy link

snakehead007 commented Jan 13, 2020

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.