1, 2, 3... Fastify!

Rate this content
Bookmark

In my journey through nodeland, I always wonder about the cost of my abstractions.


I started a journey to write an HTTP framework with extremely low overhead, and Fastify was born. With its ability to reach an astonishing 90k requests/sec, Fastify can halve your cloud server bill.


In this talk, I will walk you through the basics of the framework: how to route requests, write tests, and use the plugin system.

This talk has been presented at JSNation Live 2020, check out the latest edition of this JavaScript Conference.

FAQ

Fastify is a web framework designed for building web applications and APIs. It was started by Matteo Collina and Thomas De La Vedo in 2016.

You can install Fastify by running the npm command: 'npm install Fastify'.

For using Fastify, Node 14 is recommended because it supports the new ECMAScript Modules (ESM) features of Node.js core.

Fastify auto-load is a module that automatically loads routes from specified directories. It simplifies the management of route files by automatically registering them with the Fastify instance.

In Fastify, you can add logging by setting the logger option to true when initializing the Fastify instance. For more readable output during development, you can enable pretty printing by setting 'prettyPrint: true'.

For testing Fastify applications, the utility 'tape' is recommended. It is a simple, easy-to-use tool that supports the basic needs of testing in Fastify environments.

Fastify can handle authentication using plugins like Fastify JWT. This plugin allows you to implement JWT-based authentication by registering it with your Fastify application and configuring it with a secret key.

Fastify features include its fast performance, low overhead, lifecycle-based request and response handling instead of the traditional middleware pattern, and extensive support for modern JavaScript features like async/await.

Matteo Collina
Matteo Collina
36 min
18 Jun, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This video talk covers building web applications with Fastify, a web framework started in 2016. It guides you through installing Fastify using NPM and explains how to create a simple app that listens on port 3000. The talk emphasizes the importance of logging, showing how to enable it with 'logger: true' and use 'prettyPrint: true' for better readability. It also demonstrates how to split Fastify routes in separate files using Fastify auto-load, and how to handle authentication with Fastify JWT. The video covers unit testing with the tape utility and implementing data validation using Fluent Schema. Additionally, it touches on Fastify's lifecycle-based pattern for handling routes, its support for native Node ESM, and its performance benefits. You also learn about configuring a login system, generating JWT tokens, and testing protected routes. The speaker highlights the flexibility of Fastify and its modern JavaScript features like async/await.
Available in Español: 1, 2, 3... ¡Fastify!

1. Introduction to Fastify

Short description:

I am Matteo Collina, here to talk about Fastify. Fastify is a web framework started in 2016. Install it with NPM. Easy to use. Let's build a simple app. Import Fastify. Use Node 14. Create app. Listen on port 3000. Server started. Route not found. No logs.

Everyone, I am Matteo Collina. And I'm here today to talk to you about Fastify. Please take a moment to follow me on Twitter, at Matteo Collina. I tweet about Node.js development and a bunch of other things. So yeah, maybe you can find it interesting.

Anyway, we are here to talk about Fastify. This is, you know, we are all remote, it's all distributed around the globe. So this talk, I'm going to try something that I normally don't try on stage. So maybe let's see how it goes. So first of all, Fastify is a web framework that myself and Thomas De La Vedo started back in 2016. And now it's coming up to be the major version 3, which is going to be released soon-ish.

Anyway, how do you install it? NPM install Fastify. And it's actually very easy to use. However, we are not going to talk a lot about it, but we are going to actually use it to actually build a very, very simple app. So first of all, we can just start by, you know, having our own server and we can import Fastify. Oh, by the way, folks, I am going to use a Node 14. Why? Because I want to use the new ESM features of of Node Core. So, you know, so that's that's what you're going to use. So we can do import Fastify from Fastify. Then I can create my app. And and then I can do app.listen on port 3000. Whoa. OK. So maybe this server. Oh, and VMUse 14. Hey, so this server has started. And then I can curl it and then, oh, well, route not found. Yeah, we didn't add any. So that makes total sense. However, we also if you look at it, it also didn't log anything.

2. Adding a Logger

Short description:

We can add a logger by setting logger true. In development, set pretty print true for nicer output. The logger logs twice for each request, when it comes in and when it comes out.

So maybe we just want to add a little bit of a logger here. So which is probably useful. So we can do logger true and we can start it back. Oh, now it says server listening. It logs in new line. Delimited JSON, it use another library called Pinot to log. Still, we are getting some output out of it. However, if we are in development, what you can actually do is do pretty print true only in development. And if we do this, we will get a little bit more nicer output out of it. Note that it just logs twice for each request, logs when a request comes in and when a request comes out. Pretty nice and handy.

QnA