@prairielearn/flash
TypeScript icon, indicating that this package has built-in type declarations

1.1.9 • Public • Published

@prairielearn/flash

Adds support for flash messages to Express applications.

Usage

req.session must exist; express-session is the most common way to use sessions in Express.

import express from 'express';
import session from 'express-session';
import { flashMiddleware } from '@prairielearn/flash';

const app = express();

app.use(
  session({
    // See https://www.npmjs.com/package/express-session for more information
    // about configuring the session middleware.
    secret: 'secret',
    resave: false,
    saveUninitialized: true,
  }),
);
app.use(flashMiddleware());

Now, you can use the flash() function exported by @prairielearn/flash to read/write flash messages.

import { flash } from '@prairielearn/flash';

app.get('/set-flash', (req, res) => {
  flash('notice', 'Your preferences have been updated.');
  flash('success', 'Course created successfully.');
  flash('warning', 'Syncing completed with 5 warnings.');
  flash('error', html`Group must have <em>fewer than 10 members</em>.`);

  res.redirect('/display-flash');
});

app.get('/display-flash', (req, res) => {
  const messages = flash();
  res.json(messages);
});

The flash() function has three behaviors:

  • flash(type: string, message: string | HtmlSafeString): Set a message with the given type.
  • flash(type: string): FlashMessage[]: Get all messages with the given type.
  • flash(types: string[]): FlashMessage[]: Get all messages with any of the given types.
  • flash(): FlashMessage[]: Get all messages.

Once a message is read, it is immediately removed from the persisted list of messages. A message is only removed once it is read; it will be persisted indefinitely if it iw not.

Readme

Keywords

none

Package Sidebar

Install

npm i @prairielearn/flash

Weekly Downloads

2

Version

1.1.9

License

none

Unpacked Size

26.1 kB

Total Files

14

Last publish

Collaborators

  • nwalters512
  • mwest1066