@ fastify/ flash (英语)
闪存是会话中用于存储消息的特殊区域. 信件会写到闪存上,并在显示给用户后清除. 闪存通常与重定向结合使用,确保消息可供下页渲染.
此插件的灵感来自 连接-快闪。 。 。 .
安装
npm i @fastify/flash
使用量
闪存消息存储在会话中 。 首先,我们需要注册会话插件 : @ fastify/ security- 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"]}
})
关于会话插件的说明
@fastify/secure-session 只要会话插件:
- 注册通过请求钩
- 作为会话存储器支持 req. session
API 密码
设定闪存消息
签署
req.flash(type: string, ...message: string[] | [string[]]): number
可以用三种不同的方式称呼:
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 返回使用所提供类型存储的信件数量.
获取闪存消息
签名
reply.flash(type?: string): { [k: string]: undefined | string[] } | string[]
可以用两种不同的方式称呼:
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
许可证
许可证根据 麻省理工学院。 。 。 .