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

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Building Better Websites with Remix
React Summit Remote Edition 2021React Summit Remote Edition 2021
33 min
Building Better Websites with Remix
Top Content
Remix is a web framework built on React Router that focuses on web fundamentals, accessibility, performance, and flexibility. It delivers real HTML and SEO benefits, and allows for automatic updating of meta tags and styles. It provides features like login functionality, session management, and error handling. Remix is a server-rendered framework that can enhance sites with JavaScript but doesn't require it for basic functionality. It aims to create quality HTML-driven documents and is flexible for use with different web technologies and stacks.
Speeding Up Your React App With Less JavaScript
React Summit 2023React Summit 2023
32 min
Speeding Up Your React App With Less JavaScript
Top Content
Watch video: Speeding Up Your React App With Less JavaScript
Mishko, the creator of Angular and AngularJS, discusses the challenges of website performance and JavaScript hydration. He explains the differences between client-side and server-side rendering and introduces Quik as a solution for efficient component hydration. Mishko demonstrates examples of state management and intercommunication using Quik. He highlights the performance benefits of using Quik with React and emphasizes the importance of reducing JavaScript size for better performance. Finally, he mentions the use of QUIC in both MPA and SPA applications for improved startup performance.
Full Stack Documentation
JSNation 2022JSNation 2022
28 min
Full Stack Documentation
Top Content
The Talk discusses the shift to full-stack frameworks and the challenges of full-stack documentation. It highlights the power of interactive tutorials and the importance of user testing in software development. The Talk also introduces learn.svelte.dev, a platform for learning full-stack tools, and discusses the roadmap for SvelteKit and its documentation.
SolidJS: Why All the Suspense?
JSNation 2023JSNation 2023
28 min
SolidJS: Why All the Suspense?
Top Content
Suspense is a mechanism for orchestrating asynchronous state changes in JavaScript frameworks. It ensures async consistency in UIs and helps avoid trust erosion and inconsistencies. Suspense boundaries are used to hoist data fetching and create consistency zones based on the user interface. They can handle loading states of multiple resources and control state loading in applications. Suspense can be used for transitions, providing a smoother user experience and allowing prioritization of important content.
From GraphQL Zero to GraphQL Hero with RedwoodJS
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
From GraphQL Zero to GraphQL Hero with RedwoodJS
Top Content
Tom Pressenwurter introduces Redwood.js, a full stack app framework for building GraphQL APIs easily and maintainably. He demonstrates a Redwood.js application with a React-based front end and a Node.js API. Redwood.js offers a simplified folder structure and schema for organizing the application. It provides easy data manipulation and CRUD operations through GraphQL functions. Redwood.js allows for easy implementation of new queries and directives, including authentication and limiting access to data. It is a stable and production-ready framework that integrates well with other front-end technologies.
RedwoodJS: The Full-Stack React App Framework of Your Dreams
React Summit Remote Edition 2021React Summit Remote Edition 2021
43 min
RedwoodJS: The Full-Stack React App Framework of Your Dreams
Top Content
Redwood JS is a full stack React app framework that simplifies development and testing. It uses a directory structure to organize code and provides easy data fetching with cells. Redwood eliminates boilerplate and integrates Jest and Storybook. It supports pre-rendering and provides solutions for authentication and deployment. Redwood is a cross-client framework that allows for building web and mobile applications without duplicating work.

Workshops on related topic

Building WebApps That Light Up the Internet with QwikCity
JSNation 2023JSNation 2023
170 min
Building WebApps That Light Up the Internet with QwikCity
Featured WorkshopFree
Miško Hevery
Miško Hevery
Building instant-on web applications at scale have been elusive. Real-world sites need tracking, analytics, and complex user interfaces and interactions. We always start with the best intentions but end up with a less-than-ideal site.
QwikCity is a new meta-framework that allows you to build large-scale applications with constant startup-up performance. We will look at how to build a QwikCity application and what makes it unique. The workshop will show you how to set up a QwikCitp project. How routing works with layout. The demo application will fetch data and present it to the user in an editable form. And finally, how one can use authentication. All of the basic parts for any large-scale applications.
Along the way, we will also look at what makes Qwik unique, and how resumability enables constant startup performance no matter the application complexity.
Back to the Roots With Remix
React Summit 2023React Summit 2023
106 min
Back to the Roots With Remix
Featured Workshop
Alex Korzhikov
Pavlik Kiselev
2 authors
The modern web would be different without rich client-side applications supported by powerful frameworks: React, Angular, Vue, Lit, and many others. These frameworks rely on client-side JavaScript, which is their core. However, there are other approaches to rendering. One of them (quite old, by the way) is server-side rendering entirely without JavaScript. Let's find out if this is a good idea and how Remix can help us with it?
Prerequisites- Good understanding of JavaScript or TypeScript- It would help to have experience with React, Redux, Node.js and writing FrontEnd and BackEnd applications- Preinstall Node.js, npm- We prefer to use VSCode, but also cloud IDEs such as codesandbox (other IDEs are also ok)
Build and Deploy a Backend With Fastify & Platformatic
JSNation 2023JSNation 2023
104 min
Build and Deploy a Backend With Fastify & Platformatic
WorkshopFree
Matteo Collina
Matteo Collina
Platformatic allows you to rapidly develop GraphQL and REST APIs with minimal effort. The best part is that it also allows you to unleash the full potential of Node.js and Fastify whenever you need to. You can fully customise a Platformatic application by writing your own additional features and plugins. In the workshop, we’ll cover both our Open Source modules and our Cloud offering:- Platformatic OSS (open-source software) — Tools and libraries for rapidly building robust applications with Node.js (https://oss.platformatic.dev/).- Platformatic Cloud (currently in beta) — Our hosting platform that includes features such as preview apps, built-in metrics and integration with your Git flow (https://platformatic.dev/). 
In this workshop you'll learn how to develop APIs with Fastify and deploy them to the Platformatic Cloud.
Let AI Be Your Docs
JSNation 2024JSNation 2024
69 min
Let AI Be Your Docs
Workshop
Jesse Hall
Jesse Hall
Join our dynamic workshop to craft an AI-powered documentation portal. Learn to integrate OpenAI's ChatGPT with Next.js 14, Tailwind CSS, and cutting-edge tech to deliver instant code solutions and summaries. This hands-on session will equip you with the knowledge to revolutionize how users interact with documentation, turning tedious searches into efficient, intelligent discovery.
Key Takeaways:
- Practical experience in creating an AI-driven documentation site.- Understanding the integration of AI into user experiences.- Hands-on skills with the latest web development technologies.- Strategies for deploying and maintaining intelligent documentation resources.
Table of contents:- Introduction to AI in Documentation- Setting Up the Environment- Building the Documentation Structure- Integrating ChatGPT for Interactive Docs
Learn Fastify One Plugin at a Time
Node Congress 2021Node Congress 2021
128 min
Learn Fastify One Plugin at a Time
Workshop
Matteo Collina
Matteo Collina
Fastify is an HTTP framework for Node.js that focuses on providing a good developer experience without compromising on performance metrics. What makes Fastify special are not its technical details, but its community which is wide open for contributions of any kind. Part of the secret sauce is Fastify plugin architecture that enabled developers to write more than a hundred plugins.This hands-on workshop is structured around a series of exercises that covers from basics "hello world", to how to structure a project, perform database access and authentication.

https://github.com/nearform/the-fastify-workshop
Fastify Workshop
Node Congress 2022Node Congress 2022
152 min
Fastify Workshop
Workshop
Matteo Collina
Matteo Collina
Fastify is an HTTP framework for Node.js that focuses on providing a good developer experience without compromising on performance metrics. What makes Fastify special are not its technical details, but its community which is wide open for contributions of any kind. Part of the secret sauce is Fastify plugin architecture that enabled developers to write more than a hundred plugins. This hands-on workshop is structured around a series of exercises that covers from basics "hello world", to how to structure a project, perform database access and authentication.