@fastify/accepts
Add accepts parser to fastify
Install
npm i @fastify/accepts
Usage
const fastify = require('fastify')
const Boom = require('@hapi/boom')
fastify.register(require('@fastify/accepts'))
fastify.post('/', function (req, reply) {
const accept = req.accepts()
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
}
})
See accepts package for all available APIs.
This plugin adds to Request
object all Accepts
object methods.
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()
})
License
Licensed under MIT