Fastify Web Framework
Fastify is a high-performance web framework for Node.js, known for its low overhead and powerful plugin architecture. It provides an excellent developer experience with a focus on speed and security. Currently in stable version 5.8.5, Fastify undergoes active development with frequent releases, including critical security patches.
Common errors
-
EADDRINUSE: address already in use :::3000
cause The specified port (e.g., 3000) for the Fastify server is already being used by another process.fixClose the application currently using the port, or configure Fastify to listen on a different available port (e.g., `fastify.listen({ port: 8080 })`). -
{"statusCode":404,"error":"Not Found","message":"Route GET /your-path not found"}cause A request was made to an endpoint that does not have a defined Fastify route handler.fixDefine a `fastify.get()`, `fastify.post()`, or other HTTP method handler for the requested path, or check for typos in the route path. -
FastifyError [FST_ERR_CTP_INVALID_MEDIA_TYPE]: The Content-Type header is invalid. It must be in the form 'type/subtype[; parameter=value]'
cause An incoming request had a `Content-Type` header that did not conform to RFC 9110, specifically since Fastify v5.7.2 implemented stricter parsing.fixEnsure client requests send well-formed `Content-Type` headers. If processing non-standard types is required, register a custom content type parser using `fastify.addContentTypeParser()`. -
FastifyError [FST_ERR_REP_INVALID_PAYLOAD]: The payload is not a string or a buffer.
cause `reply.send()` was called with a payload that Fastify cannot serialize or send directly (e.g., a non-plain object, a promise that doesn't resolve to a serializable type, or an unsupported stream).fixEnsure the payload passed to `reply.send()` is a plain object, array, string, Buffer, or a Node.js readable stream. Fastify automatically JSON-stringifies plain objects/arrays.
Warnings
- breaking Fastify now strictly enforces `Content-Type` header parsing as per RFC 9110. Malformed or non-standard `Content-Type` headers that previously worked may now be rejected.
- breaking Upgrading from Fastify v4 to v5 introduces significant breaking changes. The `main` branch on GitHub now exclusively tracks v5.
- gotcha Fastify regularly releases security patches for critical vulnerabilities (CVEs). It is essential to keep your Fastify package updated to the latest stable version to ensure application security.
Install
-
npm install fastify -
yarn add fastify -
pnpm add fastify
Imports
- Fastify
const Fastify = require('fastify')import Fastify from 'fastify'
Quickstart
import Fastify from 'fastify';
const fastify = Fastify({
logger: true
});
fastify.get('/', async (request, reply) => {
reply.send({ hello: 'world' });
});
fastify.listen({ port: 3000 }, (err, address) => {
if (err) {
fastify.log.error(err);
process.exit(1);
}
console.log(`Server listening on ${address}`);
});