/fastify

Fast and low overhead web framework, for Node.js

Primary LanguageJavaScriptMIT LicenseMIT

Fastify  Build Status Coverage Status

Extremely fast node.js web framework, inspired by Express, Hapi and Restify.

Fastify is alpha software in active development, feel free to contribute!

Install

npm install fastify --save

Usage

'use strict'

const fastify = require('fastify')()
const http = require('http')
const server = http.createServer(fastify)

const schema = {
  out: {
    type: 'object',
    properties: {
      hello: {
        type: 'string'
      }
    }
  }
}

fastify
  .get('/', schema, function (req, reply) {
    reply(null, { hello: 'world' })
  })
  .get('/no-schema', function (req, reply) {
    reply(null, { hello: 'world' })
  })
  .post('/', schema, function (req, reply) {
    reply(null, { hello: 'world' })
  })

server.listen(8000, function (err) {
  if (err) {
    throw err
  }

  console.log(`server listening on ${server.address().port}`)
})

Benchmarks

As far as we know, it is one of the fastest web frameworks in town:

  • Hapi: 2200 req/sec
  • Restify: 6133 req/sec
  • Express: 8534 req/sec
  • Koa: 9640 req/sec
  • Fastify: 17140 req/sec

All benchmarks where average taken over 5 seconds, on the second run of autocannon -c 100 -d 5 -p 10 localhost:3000.

## API

fastify(req, res)

Returns a new fastify instance, which is a function with some method attached. req and res are the request and response objects from Node Core.

const fastify = require('fastify')()
const http = require('http')
const server = http.createServer(fastify)

server.listen(8000, function (err) {
  if (err) {
    throw err
  }

  console.log(`server listening on ${server.address().port}`)
})

fastify.route(options)

Options:

  • method: currently it supports only GET, POST and PUT.

  • url: the path of the url to match this route, it uses wayfarer as a router.

  • schema: an object containing the schemas for the request and response. They need to be in JSON Schema format:

    • payload: validates the body of the request if it is a POST or a PUT. It uses ajv.
    • querystring: validates the querystring. It uses ajv.
    • params: validates the params. It uses ajv.
    • out: filter and generate a schema for the response, setting a schema allows us to have 10-20% more throughput. It uses fast-json-stringify.
  • handler(request, reply(err, statusCode, object): the function that will handle this request.
    request is defined in Request.
    reply is the function that handle the response object, defined in Reply.

For POST and PUT, the incoming request body will be parsed.

Request

An object including the following properties:

  • query - the parsed querystring
  • body - the body
  • params - the params matching the URL
  • req - the incoming HTTP request from Node core

Reply

A function that accepts the following parameters:

  • error - error object (mandatory)
  • statusCode - the http status code (optional, default to 200)
  • object - JavaScript object that will be JSONified (mandatory)

fastify.get(path, [schema], handler)

Calls route with the given path, schemas and handler, setting up the GET method.

fastify.post(path, [schema], handler)

Calls route with the given path, schemas and handler, setting up the POST method.

fastify.put(path, [schema], handler)

Calls route with the given path, schemas and handler, setting up the PUT method.

fastify.register(plugin, [options], [callback])

Used to register one or more plugins.
plugin can be a single function or an array of functions.
In case of the array of functions, the same options object and callback will be passed to them.
boot-in-the-arse is used to load the plugins.
Example:

// server.js
const fastify = require('fastify')()
const http = require('http')
const server = http.createServer(fastify)

fastify.register(require('./plugin'), function (err) {
  if (err) throw err
})

const opts = {
  hello: 'world',
  something: true
}
fastify.register([
  require('./another-plugin')
  require('./yet-another-plugin')
], opts, function (err) {
  if (err) throw err
})

server.listen(8000, function (err) {
  if (err) {
    throw err
  }

  console.log(`server listening on ${server.address().port}`)
})
// plugin.js
module.exports = function (fastify, options, next) {
  fastify.get('/', schema, function (req, reply) {
    reply(null, { hello: 'world' })
  })
  next()
}

The Team

Matteo Collina

https://github.com/mcollina

https://www.npmjs.com/~matteo.collina

https://twitter.com/matteocollina

Tomas Della Vedova

https://github.com/delvedor

https://www.npmjs.com/~delvedor

https://twitter.com/delvedor

Acknowledgements

This project was kindly sponsored by nearForm.

License

Licensed under MIT.