Framework and interface layer
Fastify
Fastify is a Node.js web framework that organizes HTTP services around routes, schemas, lifecycle hooks, and encapsulated plugins.
What is Fastify and what does it do?
Fastify is a web framework for building HTTP services on Node.js. Routes connect an HTTP method and path to a handler, while schemas can describe accepted input and serialized output. Lifecycle hooks add work at defined points around that request.
The framework keeps its core focused and extends behavior through plugins. This makes it possible to group routes with the decorators, hooks, and dependencies they need instead of placing every capability in one global application scope.
A request passes through contracts and hooks
A typical request moves through several visible responsibilities:
Request -> route match -> validation -> lifecycle hooks -> handler -> serialization -> reply
JSON Schema can reject malformed input before business logic runs and can define the shape of a successful response. Hooks are useful for cross-cutting work such as authentication, request context, metrics, or cleanup. Their order and scope remain part of the service's behavior.
A small validated route
This route exposes a local health response and declares the response contract beside the handler:
import Fastify from 'fastify';
const app = Fastify();
app.get('/health', {
schema: {
response: {
200: {
type: 'object',
properties: { status: { type: 'string' } },
required: ['status']
}
}
}
}, async () => ({ status: 'ok' }));
await app.listen({ host: '127.0.0.1', port: 3000 });
The schema documents the boundary and lets Fastify serialize the expected shape. A public health endpoint would also need a deliberate exposure policy.
Plugins create ownership boundaries
Fastify plugins are encapsulated by default. A plugin can register routes, hooks, and decorators for its own branch without automatically changing unrelated branches. Dependencies between plugins should be explicit, and shared capabilities should be registered at the narrowest useful scope.
This model supports feature-oriented service structure, but excessive nesting can make registration order difficult to follow. Plugin boundaries should represent ownership rather than merely split files.
What Fastify does not decide
Fastify does not choose the database model, authentication policy, queue, deployment target, or observability strategy. Schema validation also cannot replace business authorization or database constraints. Blocking CPU work still blocks progress on the Node.js event loop unless it is moved to an appropriate execution boundary.
Fastify fits services that benefit from explicit HTTP contracts and composable scope. A service remains reliable only when timeouts, errors, shutdown, dependencies, and operational signals are designed around the framework.