Published 30th June 2020

Fastify is a Web Framework for Node.js that focuses on performance and developer experience. Fastify is similar to Express, Hapi and Restify, and is now ready for its v3.0.0 release, coming in July. The latest version is demonstrated in this video:

 

Fastify has a rich community and can be deeply customized without overhead. In the video, you’ll see fastify-autoload, which enables you to load a folder recursively! In Node 14 or 12.18, it also supports esm, allowing you to use import.

For this demo, we begin using a simple global server:

import fastify from 'fastify'

const app = fastify({

  logger: {

    prettyPrint: !!process.env.PRETTY_LOGS

  }

})

app.get('/', async function (req, reply) {

  return { hello: "world" }

})

app.listen(process.env.PORT || 3000)

And we end up with a fully backed application with automatic loading of the routes folder:

// app.js file

import { join } from 'desm'

import autoload from 'fastify-autoload'

export default async function (app, opts) {

  app.register(autoload, {

    dir: join(import.meta.url, 'routes')

  })

}

Which is also testable:

import test from 'tape'

import fastify from 'fastify'

import fp from 'fastify-plugin'

import app from './app.js'

test('load the hello world', async ({ is }) => {

  const server = fastify()

  // so we can access decorators

  server.register(fp(app))

  const res = await server.inject('/')

  is(res.body, 'hello world')

  await server.close()

})

Our route then becomes:

// routes/hello.js

export default async function (app, opts) {

  app.get('/', async () => {

    return 'hello world'

  })

}

And we can use the following to start our application:

import fastify from 'fastify'

const app = fastify({

  logger: {

    prettyPrint: !!process.env.PRETTY_LOGS

  }

})

app.register(import('./app.js'))

app.listen(process.env.PORT || 3000)

I sincerely hope you like this video introduction to Fastify.

About Fastify

Fastify was first introduced at Node.js Interactive 2017 in Vancouver in the iconic talk “Take your HTTP server to Ludicrous Speed”. The talk covers why Fastify is fast and how we achieved the results without compromising features. Some things have changed since then, but the core of the framework is still there!

If you enjoyed this video and if you would like help in bringing Fastify to production, reach out via our contact form. You can also find me on Twitter @matteocollina. You can also watch more of my demos in my Mastering Node.js Series. 

View all posts  |  Technology  |  Business  |  Culture  |  Opinion  |  Design
Follow us for more information on this and other topics.
A closer look at Fastify v3.0.0

Matteo Collina

Technical Director

Matteo Collina