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.

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

Fastify is a web framework that started in 2016 and is easy to use. It supports logging, splitting the app into separate files, and auto-loading routes. Fastify also provides authentication and user login functionality. It differs from other frameworks by fully supporting Async Await and the latest JavaScript features. Matteo Collina, the speaker, prefers Vim and T-Max as his IDE and enjoys Italian pizza as his comfort food.

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 new web framework from the creators of React Router that helps you build better, faster websites through a solid understanding of web fundamentals. Remix takes care of the heavy lifting like server rendering, code splitting, prefetching, and navigation and leaves you with the fun part: building something awesome!
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
Too much JavaScript is getting you down? New frameworks promising no JavaScript look interesting, but you have an existing React application to maintain. What if Qwik React is your answer for faster applications startup and better user experience? Qwik React allows you to easily turn your React application into a collection of islands, which can be SSRed and delayed hydrated, and in some instances, hydration skipped altogether. And all of this in an incremental way without a rewrite.
Full Stack Documentation
JSNation 2022JSNation 2022
28 min
Full Stack Documentation
Top Content
Interactive web-based tutorials have become a staple of front end frameworks, and it's easy to see why — developers love being able to try out new tools without the hassle of installing packages or cloning repos.But in the age of full stack meta-frameworks like Next, Remix and SvelteKit, these tutorials only go so far. In this talk, we'll look at how we on the Svelte team are using cutting edge web technology to rethink how we teach each other the tools of our trade.
SolidJS: Why All the Suspense?
JSNation 2023JSNation 2023
28 min
SolidJS: Why All the Suspense?
Top Content
Solid caught the eye of the frontend community by re-popularizing reactive programming with its compelling use of Signals to render without re-renders. We've seen them adopted in the past year in everything from Preact to Angular. Signals offer a powerful set of primitives that ensure that your UI is in sync with your state independent of components. A universal language for the frontend user interface.
But what about Async? How do we manage to orchestrate data loading and mutation, server rendering, and streaming? Ryan Carniato, creator of SolidJS, takes a look at a different primitive. One that is often misunderstood but is as powerful in its use. Join him as he shows what all the Suspense is about.
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
We all love GraphQL, but it can be daunting to get a server up and running and keep your code organized, maintainable, and testable over the long term. No more! Come watch as I go from an empty directory to a fully fledged GraphQL API in minutes flat. Plus, see how easy it is to use and create directives to clean up your code even more. You're gonna love GraphQL even more once you make things Redwood Easy!
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
Tired of rebuilding your React-based web framework from scratch for every new project? You're in luck! RedwoodJS is a full-stack web application framework (think Rails but for JS/TS devs) based on React, Apollo GraphQL, and Prisma 2. We do the heavy integration work so you don't have to. We also beautifully integrate Jest and Storybook, and offer built-in solutions for declarative data fetching, authentication, pre-rendering, logging, a11y, and tons more. Deploy to Netlify, Vercel, or go oldschool on AWS or bare metal. In this talk you'll learn about the RedwoodJS architecture, see core features in action, and walk away with a sense of wonder and awe in your heart.

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.
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.
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