@ fastify/接受
添加一个接受解析器以快速化 .
安装
npm i @fastify/accepts
兼容性
| 插件版本 | 快速化版本 |
|---|---|
>=5.x | ^5.x |
^4.x | ^4.x |
>=2.x <4.x | ^3.x |
^1.x | ^2.x |
^1.x | ^1.x |
请注意,如果一个快取版本没有支持,那么该插件的相应版本也一样 上表列出. 见 快速化 LTS 政策 更多细节.
使用量
const fastify = require('fastify')
const Boom = require('@hapi/boom')
fastify.register(require('@fastify/accepts'))
fastify.post('/', function (req, reply) {
const accept = req.accepts() // Accepts object
switch(accept.type(['json', 'html'])) {
case 'json':
reply.type('application/json').send({hello: 'world'})
break
case 'html':
reply.type('text/html').send('<b>hello, world!</b>')
break
default:
reply.send(Boom.notAcceptable('unacceptable'))
break
}
})
见 接受软件包 用于所有可用的API.
此插件添加到 Request 全部对象 Accepts 对象方法.
fastify.post('/', function (req, reply) {
req.charset(['utf-8'])
req.charsets()
req.encoding(['gzip', 'compress'])
req.encodings()
req.language(['es', 'en'])
req.languages()
req.type(['image/png', 'image/tiff'])
req.types()
})
选项
decorateReply若为true,则Reply对象将随requestAccepts, (中文(简体) ),requestTypes, (中文(简体) ),requestCharsets, (中文(简体) ),requestEncodings,以及requestLanguages方法,从Request对象。 默认 :false。 。 。 .
许可证
许可证根据 麻省理工学院。 。 。 .