@fastify/flash

中文文档
6.0.6发布于 2026/10/10MITCommonJS构建来源已验证近 30 天下载 32.6万

Flash message plugin for fastify.

原始使用文档
文档目录7 节
  1. Install
  2. Usage
  3. Note on session plugin
  4. API
  5. Set flash messages
  6. Get flash messages
  7. License

@fastify/flash

CI NPM version js-standard-style

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.

Install

npm i @fastify/flash

Usage

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 https://github.com/fastify/fastify-cookie
    path: "/"
  }
})
fastify.register(fastifyFlash, {
  prefix: "/"
})

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

API

Set flash messages

Signature

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 stored with the provided type.

Get flash messages

signature

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 an array of messages that are stored with the provided type

License

Licensed under MIT.

版本与发布记录

@fastify/flash 共发布 14 个版本,首次发布于 2022/04/27;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
6.0.6latest2026/10/101 个月前
6.0.52026/08/143 个月前
6.0.42026/08/133 个月前
6.0.32025/01/111.8 年前
6.0.22024/11/062 年前
6.0.12024/11/012 年前

其他发布通道:next → 6.0.0。

关于 @fastify/flash 的常见问题

@fastify/flash 是什么?

Flash message plugin for fastify.

如何安装 @fastify/flash?

在项目目录运行 npm install @fastify/flash,或使用 pnpm add / yarn add / bun add。

@fastify/flash 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/fastify__flash 是否存在。

@fastify/flash 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fastify/flash 可以商用吗?

@fastify/flash 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fastify/flash 的下载量有多少?

根据 npm 官方下载统计,@fastify/flash 在 2026/10/15 – 2026/11/13 这 30 天内被下载 326,314 次,日均约 10,877 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fastify/flash 还在维护吗?

最新版本 6.0.6 发布于 2026/10/10(1 个月前);至今共发布 14 个版本,其中过去 12 个月发布了 3 个;首次发布于 2022/04/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。