Defaults for Fastify that everyone can agree on
Switch branches/tags
Clone or download
Latest commit 7373513 Dec 17, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib Add assert.ok Dec 15, 2018
test Add assert.ok Dec 15, 2018
.gitignore Updated .gitignore Apr 1, 2018
.travis.yml Updated travis Dec 14, 2018
LICENSE Initial commit Apr 1, 2018 Add to docs Dec 15, 2018
index.js Added decorator Apr 6, 2018
package.json 0.3.0 Dec 17, 2018


js-standard-style Build Status

Defaults for Fastify that everyone can agree on™.
This plugins adds some useful utilities to your Fastify instance, see the API section to learn more.

Why these APIs are here and not directly into core?
Because Fastify aims to be as small and focused as possible, every utility that is not essential should be shipped as standalone plugin.


npm i fastify-sensible


const fastify = require('fastify')()

fastify.get('/', (req, reply) => {

fastify.get('/async', async (req, reply) => {
  throw fastify.httpErrors.notFound()




Object that exposes all the 4xx and 5xx error constructors: usage:

 // the custom message is optional
const notFoundErr = fastify.httpErrors.notFound('custom message')


  • fastify.httpErrors.badRequest()
  • fastify.httpErrors.unauthorized()
  • fastify.httpErrors.paymentRequired()
  • fastify.httpErrors.forbidden()
  • fastify.httpErrors.notFound()
  • fastify.httpErrors.methodNotAllowed()
  • fastify.httpErrors.notAcceptable()
  • fastify.httpErrors.proxyAuthenticationRequired()
  • fastify.httpErrors.requestTimeout()
  • fastify.httpErrors.conflict()
  • fastify.httpErrors.gone()
  • fastify.httpErrors.lengthRequired()
  • fastify.httpErrors.preconditionFailed()
  • fastify.httpErrors.payloadTooLarge()
  • fastify.httpErrors.uriTooLong()
  • fastify.httpErrors.unsupportedMediaType()
  • fastify.httpErrors.rangeNotSatisfiable()
  • fastify.httpErrors.expectationFailed()
  • fastify.httpErrors.imateapot()
  • fastify.httpErrors.misdirectedRequest()
  • fastify.httpErrors.unprocessableEntity()
  • fastify.httpErrors.locked()
  • fastify.httpErrors.failedDependency()
  • fastify.httpErrors.unorderedCollection()
  • fastify.httpErrors.upgradeRequired()
  • fastify.httpErrors.preconditionRequired()
  • fastify.httpErrors.tooManyRequests()
  • fastify.httpErrors.requestHeaderFieldsTooLarge()
  • fastify.httpErrors.unavailableForLegalReasons()


  • fastify.httpErrors.internalServerError()
  • fastify.httpErrors.notImplemented()
  • fastify.httpErrors.badGateway()
  • fastify.httpErrors.serviceUnavailable()
  • fastify.httpErrors.gatewayTimeout()
  • fastify.httpErrors.httpVersionNotSupported()
  • fastify.httpErrors.variantAlsoNegotiates()
  • fastify.httpErrors.insufficientStorage()
  • fastify.httpErrors.loopDetected()
  • fastify.httpErrors.bandwidthLimitExceeded()
  • fastify.httpErrors.notExtended()
  • fastify.httpErrors.networkAuthenticationRequired()


The reply interface is decorated with all the functions declared above, use it is very easy:

fastify.get('/', (req, reply) => {


The reply interface is decorated with jshttp/vary, the API is the same, but you don't need to pass the res object.

fastify.get('/', (req, reply) => {


The request interface is decorated with jshttp/forwarded, the API is the same, but you don't need to pass the request object.

fastify.get('/', (req, reply) => {


The request interface is decorated with jshttp/proxy-addr, the API is the same, but you don't need to pass the request object.

fastify.get('/', (req, reply) => {
  reply.send(req.proxyaddr(addr => addr === ''))

The request interface is decorated with jshttp/type-is, the API is the same, but you don't need to pass the request object.

fastify.get('/', (req, reply) => {
  reply.send(['html', 'json']))


Verify if a given condition is true, if not it throws the specified http error.
Very useful if you work with async routes.

// the custom message is optional
  req.headers.authorization, 400, 'Missing authorization header'

The assert API exposes also the following methods:

  • fastify.assert.ok()
  • fastify.assert.equal()
  • fastify.assert.notEqual()
  • fastify.assert.strictEqual()
  • fastify.assert.notStrictEqual()
  • fastify.assert.deepEqual()
  • fastify.assert.notDeepEqual()


Async await wrapper for easy error handling without try-catch, inspired by await-to-js

const [err, user] = await
  db.findOne({ user: 'tyrion' })

Custom error handler

This plugins also adds a custom error handler which hides the error message in case of 500 errors, instead it returns Something went wrong.
This is especially useful if you are using async routes, where every uncaught error will be sent back to the user (but dot not worry, the original error message is logged as error in any case).


Do you feel there is some utility that everyone can agree on which is not present?
Open an issue and let's discuss it! Even better a pull request!


The project name is inspired by vim-sensible, an awesome package that if you use vim you should use too.


MIT Copyright © 2018 Tomas Della Vedova