Contents
Fastify
A fast web framework for Node.js: routes, validation schemas, plugins and hooks.
1.Getting started
- What Fastify isWhy you need the Fastify framework, what makes it fast and how it differs from Express.
- Installing and the first serverCreating a project, installing Fastify, the fastify() function, starting with listen and the built-in logger.
- Project structureWhy app.js and server.js are separate, and where routes and plugins live.
2.Routes and replies
- Routes and HTTP methodsHow to declare GET, POST, PUT and DELETE routes, the full app.route form and prefixes.
- Route parameters, query and bodyHow to read route parameters, the query string and the JSON request body through the request object.
- Replies: replyHow to set the status code and headers, send a reply with reply.send and redirect.
3.Schemas
4.Plugins and hooks
- Plugins and encapsulationWhat a Fastify plugin is, how to register it, pass options, and how encapsulation works.
- fastify-plugin and decoratorsHow to add a shared object with decorate, a request property with decorateRequest, and make them visible to all plugins.
- HooksHow to run code at different stages of a request: onRequest, preHandler, onSend, onResponse and onClose.
- Error handlingHow to throw HTTP errors, use httpErrors from @fastify/sensible and set up setErrorHandler and setNotFoundHandler.
- Logging with PinoLog levels, request.log, readable output with pino-pretty and hiding secrets in logs.
5.Data
- Configuration and environment variablesHow to keep the port, database path and secrets in a .env file, load them with --env-file and check them with @fastify/env.
- Connecting a databaseHow to connect SQLite through better-sqlite3 as a Fastify plugin, create tables and close the connection on shutdown.
- A CRUD API for postsThe full set of post routes for the blog: list, single post, create, update and delete with schemas and an SQLite database.
6.Security and files
- Authentication with JWTHow to issue a token on login with @fastify/jwt and protect the post-editing routes from strangers.
- CORSWhat CORS is, why the browser blocks requests to an API on another address, and how to allow them with @fastify/cors.
- Security headers and rate limitingHow to add protective headers with @fastify/helmet and limit the number of requests with @fastify/rate-limit.
- Static filesHow to serve HTML, CSS, images and other ready-made files with @fastify/static.
- File uploadsHow to accept a file from a client with @fastify/multipart, save it to disk and serve it by a link.
7.TypeScript, tests and deployment
- TypeScript with FastifyHow to run Fastify with TypeScript, type route parameters, derive types from a schema with a type provider and describe decorators.
- Testing: inject and node:testHow to test Fastify routes without starting a server, using app.inject() and the node:test module built into Node.js.
- Running on a serverHow to prepare Fastify for a production server: the 0.0.0.0 address, the PM2 process manager and graceful shutdown.