Miss any of our Open RFC calls?Watch the recordings here! »

fastify-accepts

2.0.1 • Public • Published

fastify-accepts

CI workflow JavaScript Style Guide Known Vulnerabilities

Add accepts parser to fastify

Install

npm install --save fastify-accepts

Usage

const fastify = require('fastify')
const Boom = require('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
  }
})

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

Keywords

none

Install

npm i fastify-accepts

DownloadsWeekly Downloads

9,906

Version

2.0.1

License

MIT

Unpacked Size

11.5 kB

Total Files

10

Last publish

Collaborators

  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar