difference between flash, connect-flash and express-flash
Asked Answered
G

2

18

I'm still kinda confused on what exactly is the difference between flash, connect-flash and express-flash.

Installation:

  • flashnpm install flash

  • express-flash : npm install express-flash

  • connect-flash :npm install connect-flash

Usage:

flash:

app.use(session()); // session middleware 
app.use(require('flash')());

app.use(function (req, res) {
  // flash a message 
  req.flash('info', 'hello!');
  next();
})

connect-flash

var flash = require('connect-flash');
var app = express();

app.configure(function() {
  app.use(express.cookieParser('keyboard cat'));
  app.use(express.session({ cookie: { maxAge: 60000 }}));
  app.use(flash());
});

express-flash It even request that the usage should set up the same way you would connect-flash:

var flash = require('express-flash'),
    express = require('express'),
    app = express();

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

Can someone please explain?

Groundwork answered 8/11, 2017 at 10:15 Comment(0)
F
20

There really is no drastic difference between the three packages. They all accomplish the same thing in their own way. The difference between the three are:

  1. flash is written by the Express team, making it an official middleware for Express.
  2. connect-flash as stated from the README:

This middleware was extracted from Express 2.x

So in a sense this is similar to flash except a legacy version of it from Express 2.x days. However, the name suggests it was meant for the Connect framework, but usually any connect-* packages work fine with Express.

  1. express-flash is just a wrapper around connect-flash. You can see that in the source code here.

Out of all three, connect-flash seems to be the most used judging from npm stats.

Flocculate answered 8/11, 2017 at 16:16 Comment(1)
connect-flash hasn't been updated since 2013 except for an advertisement added to the README.Elfredaelfrida
P
0
  • connect-flash helps you deal with error when you req.session.destroy() and res.render('view-name')
  • if you destroy session before calling res.render('view-name'), you'll get "Error: req.flash() requires sessions" even if you don't use set or get flash at all.

I am using pug template and ran into this problem, and after I switch from express-flash to connect-flash, the error goes away.

I hope this can help someone who run into the same problem as I.

Paulo answered 1/7, 2022 at 1:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.