npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-flash-message

v3.0.2

Published

Provides Express.js flash message middleware that work for rendering or redirecting.

Downloads

3,126

Readme

express-flash-message v3

Provides Express.js flash message middleware that work for rendering or redirecting.

Requires express-sessions or cookie-session middleware before apply this middleware.

installation

npm: npm install --save express-flash-message

yarn: yarn add express-flash-message

pnpm: pnpm add express-flash-message

Note: if you don't install express-session or cookie-session before, then you also need install one of these dependencies.

usage

You can go to example folder to run the following example.

import express from 'express';
import path, { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import nunjucks from 'nunjucks';
import session from 'express-session';
import flash from 'express-flash-message';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const app = express();

// setup view
app.engine('njk', nunjucks.render);
app.set('view engine', 'njk');
nunjucks.configure(path.resolve(__dirname, '../views'), {
  autoescape: true,
  express: app,
});

// setup express-session
app.use(
  session({
    secret: 'secret',
    resave: false,
    saveUninitialized: true,
    cookie: {
      maxAge: 1000 * 60 * 60 * 24 * 7, // 1 week
      // secure: true, // becareful set this option, check here: https://www.npmjs.com/package/express-session#cookiesecure. In local, if you set this to true, you won't receive flash as you are using `http` in local, but http is not secure
    },
  })
);

// setup flash
app.use(
  flash({
    sessionKeyName: 'express-flash-message',
    // below are optional property you can pass in to track
    onAddFlash: (type, message) => {},
    onConsumeFlash: (type: string, messages: string[]) => {},
  })
);

app.get('/flash', (req, res) => {
  res.flash('success', 'this is info flash message');
  res.flash('success', 'this is info flash message2');
  setTimeout(() => {
    res.redirect('/');
  }, 3000);
});

app.get('/', (req, res) => {
  res.flash('success', '-----info----');
  res.render('index');
});

app.listen('5000', () => {
  console.log('app start at http://localhost:5000');
});

Then in your template, you can consume the flash messages by calling getFlashMessage(type). But please remember the messages is an array.

<ul>
  {% for msg in getFlashMessages('success') %}
  <li>{{msg}}</li>
  {% endfor %}
</ul>

Note the example above is using nunjuck engine. You can use other engines also.

Note:

  • The flash message is an array. You can use res.flash('type', 'value') several times and all the value will be stored in the key. Then when you call getFlashMessages('type') in your template directly, it will give you an array which contains all the values you want to flash.

  • The Flash message will be consumed after you call getFlashMessages('type').

  • The package is build with typescript, so if you are using CommonJS and need require import, then you have to require it this way: const flash = require('express-flash-message').default;

migrate from V2

If you are using v2 version, check doc here. Difference between v2 and v3:

  1. in v3, we are using response.flash instead of request.flash.

  2. If you are using template, you don't need to call consumeFlash method, it integrate with response.render now. In your template, you can directly call getFlashMessages(key) to get the flash message.

  3. If you still want to consume the flash by your self, you can call the following method which exported by the package.

export const getFlashMessages = (
  req: Request,
  sessionKeyName: string,
  type: string,
  onConsumeFlash?: OnConsumeFlash
) => void

License

MIT