Skip to content

Instantly share code, notes, and snippets.

@abou7mied
Created December 26, 2019 06:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abou7mied/12726c87da398dbe1119d04d41500788 to your computer and use it in GitHub Desktop.
Save abou7mied/12726c87da398dbe1119d04d41500788 to your computer and use it in GitHub Desktop.
connect-flash + hbs example
<html>
<body>
{{#each messages}}
Message: {{this}}
{{/each}}
</body>
</html>
const flash = require('connect-flash');
const express = require('express');
const hbs = require('hbs');
const session = require('express-session');
const cookieParser = require('cookie-parser');
const app = express();
app.set('view engine', 'hbs');
app.use(cookieParser('keyboard cat'));
app.use(session({ cookie: { maxAge: 60000 } }));
app.use(flash());
hbs.localsAsTemplateData(app);
app.get('/flash', function (req, res) {
// Set a flash message by passing the key, followed by the value, to req.flash().
req.flash('info', 'Flash is back!');
res.redirect('/');
});
app.get('/', function (req, res) {
res.render('index', { messages: req.flash('info') });
});
app.listen(3000);
@HosMercury
Copy link

Great simple

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