| name | fastify-best-practices |
| description | Develop, configure, and debug Fastify (Node.js/TypeScript) servers and REST APIs — routes, plugins, JSON Schema validation, hooks, auth, serialization, performance, deployment. Triggers: Fastify, server.ts, app.ts, fastify.config. |
Quick Start
A minimal, runnable Fastify server to get started immediately:
import Fastify from 'fastify'
const app = Fastify({ logger: true })
app.get('/health', async (request, reply) => {
return { status: 'ok' }
})
const start = async () => {
await app.listen({ port: 3000, host: '0.0.0.0' })
}
start()
Recommended Reading Order for Common Scenarios
- New to Fastify? Start with
plugins.md → routes.md → schemas.md
- Adding authentication:
plugins.md → hooks.md → authentication.md
- Improving performance:
schemas.md → serialization.md → performance.md
- Setting up testing:
routes.md → testing.md
- Going to production:
logging.md → configuration.md → deployment.md
How to use
Read individual rule files for detailed explanations and code examples:
Core Principles
- Encapsulation: Fastify's plugin system provides automatic encapsulation
- Schema-first: Define schemas for validation and serialization
- Performance: Fastify is optimized for speed; use its features correctly
- Async/await: All handlers and hooks support async functions
- Minimal dependencies: Prefer Fastify's built-in features and official plugins