Skip to content
No description, website, or topics provided.
Branch: master
Clone or download
fox1t Merge pull request #4 from fastify/develop
Refactors internal typings not being exposed
Latest commit 210837e Mar 19, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
tests Fixes typings not being exposed after publish Mar 19, 2019
.editorconfig First implementation Mar 5, 2019
.gitignore Refactors types and project structure Mar 15, 2019
LICENSE Initial commit Mar 2, 2019


The flash is a special area of the session used for storing messages. Messages are written to the flash and cleared after being displayed to the user. The flash is typically used in combination with redirects, ensuring that the message is available to the next page that is to be rendered.

This plugin is inspired by connect-flash.


npm install fastify-flash


Flash messages are stored in the session. First we need to register the session plugin: fastify-secure-session.

const fastify = require('fastify')()
const fastifySession = require('fastify-secure-session')
const fastifyFlash = require('fastify-flash')

fastify.register(fastifySession, {
  // adapt this to point to the directory where secret-key is located
  key: fs.readFileSync(path.join(__dirname, 'secret-key')),
  cookie: {
    // options from setCookie, see

fastify.get('/test', (req, reply) => {
  req.flash('warning', ['username required', 'password required'])

  const warning = reply.flash('warning')
  reply.send({ warning }) // {"warning":["username required","password required"]}

Note on session plugin

fastify-secure-session can be replaced by any session plugin as long as it:

  1. registers via the onRequest hook
  2. supports req.session as the session store


Set a flash massage(s)


req.flash(type: string, ...message: string[] | [string[]]): number

It can be called in three different ways:

  • req.flash('info', 'Welcome back')
  • req.flash('warning', ['username required', 'password required'])
  • req.flash('info', 'Hello %s', 'Jared') // will use util.format to format the string

req.flash returns the number of messages store with provided type.

Get a flash message(s)


reply.flash(type?: string): { [k: string]: undefined | string[] } | string[]

It can be called in two different ways:

  • reply.flash() // returns all messages as object { [k: string]: undefined | string[] }
  • reply.flash('info') // returns array of messages that are stored with provided type



You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.