Skip to content

Instantly share code, notes, and snippets.

@raddeus
Last active May 24, 2023 15:41
Show Gist options
  • Save raddeus/11061808 to your computer and use it in GitHub Desktop.
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"
}
}
@kdssoftware
Copy link

kdssoftware commented Mar 20, 2020

@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
Copy link

thinhbg commented Mar 21, 2020 via email

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