Skip to content

Instantly share code, notes, and snippets.

@raddeus
Last active February 4, 2025 09:42
Show Gist options
  • Select an option

  • Save raddeus/11061808 to your computer and use it in GitHub Desktop.

Select an option

Save raddeus/11061808 to your computer and use it in GitHub Desktop.
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"
}
}
@aimuzov

aimuzov commented Jul 11, 2014

Copy link
Copy Markdown

Thank you, this example helped me.

@jmcbee

jmcbee commented Sep 29, 2014

Copy link
Copy Markdown

excellent.

@sespinosa

Copy link
Copy Markdown

ty sir

@IvanMMM

IvanMMM commented Apr 13, 2015

Copy link
Copy Markdown

Thanks :)

@Twanawebtech

Copy link
Copy Markdown

awesome dude thanks

@colepacak

Copy link
Copy Markdown

So helpful. Thank you.

ghost commented Dec 2, 2016

Copy link
Copy Markdown

beautiful.

@Adizbek

Adizbek commented May 6, 2017

Copy link
Copy Markdown

Thanks.

@rshamsy

rshamsy commented Sep 24, 2018

Copy link
Copy Markdown

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

@sayeed-ahmed-rasel

Copy link
Copy Markdown

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

@tezeoffor

Copy link
Copy Markdown

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

ghost commented Sep 16, 2019

Copy link
Copy Markdown

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

raddeus commented Sep 16, 2019

Copy link
Copy Markdown
Author

@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).

@kdssoftware

Copy link
Copy Markdown

thanks

@thinhbg

thinhbg commented Mar 20, 2020

Copy link
Copy Markdown

What is the front end code sir ?

@kdssoftware

kdssoftware commented Mar 20, 2020

Copy link
Copy Markdown

@thinhbg My front ent is this (in Pug using bootstrap):

.row.justify-content-center
    #messages.col-12
        if messages.test
            each message in messages.test
                div.alert-message(class="alert alert-success alert-dismissible fade show" role="alert")=message
                    button(type="button" class="close" data-dismiss="alert" aria-label="Close")
                        span(aria-hidden="true") ×

But in this gist, the code redirect to a JSON output of the req.flash('test') so no front-end needed. But my Pug snippet here, will show the each message of "test". But only if you change the app.all('/test') render function.

@thinhbg

thinhbg commented Mar 21, 2020 via email

Copy link
Copy Markdown

@PoojaMaulekhi

Copy link
Copy Markdown

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

Hlw,
I am facing same error .
Have you get any solution .Please help me to get the solution .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment