How to send flash messages in Express 4.0? How to send flash messages in Express 4.0? express express

How to send flash messages in Express 4.0?


This Gist should answer your question:

https://gist.github.com/raddeus/11061808

in your application setup file:

app.use(flash());

Put that right after you set up your session and cookie parser. That's really all you should need to use flash.

You are using:

req.flash('signupMessage', anyValue);

before redirecting to /signup right?

Here's a fun little tidbit that I currently use for a personal site(in my main application file):

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

Now every view will have access to any error or success messages that you flash. Works well for me.

One final thing (this is nitpicky but you may gain some knowledge). If you change:

<% if (message.length > 0) { %>

to:

<% if (message) { %>

It will work the same way but will not fail if message is undefined. undefined and empty strings are both considered "falsy" values in javascript.

EDIT: My cookie/session/flash setup goes as follows:

app.use(cookieParser('secretString'));app.use(session({cookie: { maxAge: 60000 }}));app.use(flash());

Maybe seeing your application setup code would help. Also note that using app.configure is no longer necessary in Express 4.

Final edit: https://gist.github.com/raddeus/11061808

That is a working example. Go to localhost:3000 after running that app and you should see ['it worked'] on your screen.


https://gist.github.com/brianmacarthur/a4e3e0093d368aa8e423

I, too, was initially confused by flash messages in Express 4. The confusion for me arose partly from the distinction between the concept of flash messaging, a temporary message available to a template, and the various implementations of flash messaging, which include express-flash, other modules, and custom middleware.

Just to expand on the excellent response from Thad Blankenship above, I created a Gist for the beginner that includes two approaches to flash messages--the express-flash module and custom middleware--rendered in jade, ejs, or handlebars.

The readme includes details about the getter--req.flash(type)--and setter--req.flash(type, message)--methods exposed by express-flash and how they differ from the exploitation of the res.locals and req.session objects exposed by express-session in the custom middleware.


To show flash message you have to install flash module in your project using cmd.

npm install express-session --savenpm install cookie-parser --savenpm install connect-flash --save

Now you have to add some code to the app.js file to access those modules. Let’s add these code.

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());

Now generate flash message

req.flash('success', 'Registration successfully');res.locals.message = req.flash();

To show the flash message in view file use the code

<% if(locals.message){ %>    <div class="alert alert-success" role="alert">        <strong>Well done!</strong> <%=message.success%>    </div><% } %>