Getting started

Introduction

Fastify lets you create HTTP servers in Node.js with good performance. This guide targets fastify v0.28.x.

Hello world

const fastify = require('fastify')()

fastify.get('/', (req, reply) => {
  reply.send({ hello: 'world' })
})

fastify.listen(3000, err => {
  if (err) throw err
  const port = fastify.server.address().port
  console.log(`server listening on ${port}`)
})

Plugins

app.js

fastify.register(require('./route'))

route.js

function (fastify, opts, next) {
  fastify.get('/', (req, reply) => {
    reply.send({ hello: 'world' })
  })

  next()
})

Compose your app functionality into plugins. Plugins are simply functions.

See: Plugins

Routes

Writing routes

fastify.route({
  method: 'GET',
  url: '/',
  schema: { ··· },
  handler: (req, reply) => { ··· }
  beforeHandler: (req, reply, done) => { ··· }
})

Shorthand declarations

fastify.get(path, [options], handler)
fastify.head(···)
fastify.post(···)
fastify.put(···)
fastify.delete(···)
fastify.options(···)
fastify.patch(···)

Async/await

fastify.get('/', options, async (req, reply) => {
  return data
  // or
  reply.send(data)
})

When using async functions, you can either return data or use reply.send.

Request/reply

Request

request.query
request.body
request.params
request.headers
request.req  // Node.js core
request.log.info('hello')

See: Request

Reply

Response headers

reply.code(404)
reply.header('Content-Type', 'text/html')
reply.type('text/html')

Redirects

reply.redirect('/foo')
reply.redirect(302, '/foo')

Sending

reply.send(payload)
reply.sent // → true|false

See: Reply

JSON schema

Define a JSON schema

const schema = {
  querystring: {
    name: { type: 'string' },
    excitement: { type: 'integer' }
  },
  response: {
    200: {
      type: 'object',
      properties: {
        hello: { type: 'string' }
      }
    }
  }
}

Pass it to the route

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

or (same as above)

fastify.route({
  method: 'GET',
  url: '/',
  schema,
  handler: (req, reply) => { ··· }
})

By defining a JSON schema, you get validation and improved performance.

See: Validation and serialization

Plugins

With function

fastify.register(
  require('./route'),
  err => { if (err) throw err }
)

route.js

module.exports = (fastify, options, next) => {
  fastify.get('/', ···)
  next()
}

See: Register

Multiple

fastify.register([
  require('./another-route'),
  require('./yet-another-route')
], opts, (err) => {
  if (err) throw err
})

You can pass arrays to register().

Register with prefix

fastify.register(
  require('./route'),
  { prefix: '/v1' }
)

This prefixes all routes in that module.

Helmet

const helmet = require('fastify-helmet')

fastify.register(helmet)

See: fastify-helmet

fastify-plugin

const fp = require('fastify-plugin')

module.exports = fp((fastify, opts, next) => {
  // your plugin code
  fastify.decorate('utility', () => {})

  next()
}, '0.x')

Allows you to limit Fastify versions via semver, and allows you not make a new Fastify scope.

See: fastify-plugin

Decorators

Middleware

Middleware

fastify.use(require('cors')())
fastify.use(require('dns-prefetch-control')())
fastify.use(require('frameguard')())
fastify.use(require('hide-powered-by')())
fastify.use(require('hsts')())
fastify.use(require('ienoopen')())
fastify.use(require('x-xss-protection')())

Compatible with Express and Restify middlewares. (Don’t use these middleware, these are covered by fastify-helmet.)

See: Middlewares

Template rendering

point-of-view

const fastify = require('fastify')()

fastify.register(require('point-of-view'), {
  engine: {
    ejs: require('ejs')
  }
})
fastify.get('/', (req, reply) => {
  reply.view('/templates/index.ejs', { text: 'text' })
})

Support ejs, pug, handlebars and marko.

See: point-of-view

Options

fastify.register(require('point-of-view'), {
  engine: {
    ejs: require('ejs')
  },
  templates: '/templates',
  options: {}
})

templates lets you update the templates folder. options are options passed onto the template engines.

0 Comments for this cheatsheet. Write yours!