Skip to content

A closer look at Fastify v3.0.0

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:

Plain Text
import fastify from 'fastify'</p><p>const app = fastify({</p><p>  logger: {</p><p>    prettyPrint: !!process.env.PRETTY_LOGS</p><p>  }</p><p>})</p><p>app.get('/', async function (req, reply) {</p><p>  return { hello: "world" }</p><p>})</p><p>app.listen(process.env.PORT || 3000)

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

Plain Text
// app.js file</p><p>import { join } from 'desm'</p><p>import autoload from 'fastify-autoload'</p><p>export default async function (app, opts) {</p><p>  app.register(autoload, {</p><p>    dir: join(import.meta.url, 'routes')</p><p>  })</p><p>}

Which is also testable:

Plain Text
import test from 'tape'</p><p>import fastify from 'fastify'</p><p>import fp from 'fastify-plugin'</p><p>import app from './app.js'</p><p>test('load the hello world', async ({ is }) => {</p><p>  const server = fastify()</p><p>  // so we can access decorators</p><p>  server.register(fp(app))</p><p>  const res = await server.inject('/')</p><p>  is(res.body, 'hello world')</p><p>  await server.close()</p><p>})

Our route then becomes:

Plain Text
// routes/hello.js</p><p>export default async function (app, opts) {</p><p>  app.get('/', async () => {</p><p>    return 'hello world'</p><p>  })</p><p>}

And we can use the following to start our application:

Plain Text
import fastify from 'fastify'</p><p>const app = fastify({</p><p>  logger: {</p><p>    prettyPrint: !!process.env.PRETTY_LOGS</p><p>  }</p><p>})</p><p>app.register(import('./app.js'))</p><p>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. 

Insight, imagination and expertly engineered solutions to accelerate and sustain progress.