Provides Express.js flash message middleware that work for rendering or redirecting.
Requires sessions middleware before apply this middleware.
npm install express-flash-message
yarn add express-flash-message
;;app.useflash; // if you don't provide sessionKeyName, then it will use 'flash' to store in your sessison.app.get'/flash',;app.get'/',;
- this middleware must be used after session middleware.
req.consumeFlashreturns a Promise. We saw several other flash packages are not using async process to deal with flash message, then if user refresh page really quickly, it will have trouble to consume the flash message. This package is mainly to fix that issue.
- the flash message is an array. You can use
await req.flash('key', 'value')several times and all the value will be stored to the
key. Then when you call
await req.consumeFlash('key'), it will give you an array which contains all the value you want to flash.
The Falsh message will be set to null after you call
await req.consumeFlash('key') from session which means it will be removed from your session.