fastify
Installation
SKILL.md
Apply these conventions when working with Fastify code in node.js projects.
Server Setup
- Create the Fastify instance with explicit options:
logger: true(or Pino config object),trustProxywhen behind a reverse proxy. - Separate app construction (
app.ts) from server startup (server.ts/cluster.ts) to enable testing viainject()without starting the server.
Plugins & Encapsulation
- Everything is a plugin. Register features with
fastify.register(plugin, options). - Plugins are encapsulated by default — decorators, hooks, and routes registered inside a plugin are invisible to parent scopes.
- Use
fastify-pluginwrapper only when a plugin must expose decorators or hooks to the parent scope (e.g., database connection, authentication decorator). - Declare decorator dependencies in the
dependenciesarray to fail fast at boot, not at runtime. - Plugin loading order matters — plugins registered first are available to later plugins in the same scope.
Routes
- Use shorthand methods:
fastify.get(),fastify.post(), etc. - Always use
asynchandlers thatreturnthe response body. Do not mixreturnwithreply.send()— pick one. - Group related routes in a plugin and apply a
prefixviaregisteroptions. - Use route-level
schemafor request validation (body,querystring,params,headers) and response serialization (response).