fastify-best-practices
Comprehensive guidance for building Fastify Node.js backends with TypeScript, covering routes, plugins, validation, and production deployment.
- Covers 16+ core topics including plugins, route organization, JSON Schema validation, error handling, hooks, authentication, testing, logging with Pino, and TypeScript integration via strip types
- Includes recommended reading paths for common scenarios: new projects, adding authentication, performance optimization, testing setup, and production deployment
- Addresses full request lifecycle management, response serialization, CORS and security headers, WebSocket support, database integration, and HTTP proxying
- Emphasizes schema-first development, plugin encapsulation, async/await patterns, and Fastify's built-in performance optimizations with minimal external dependencies
When to use
Use this skill when you need to:
- Develop backend applications using Fastify
- Implement Fastify plugins and route handlers
- Get guidance on Fastify architecture and patterns
- Use TypeScript with Fastify (strip types)
- Implement testing with Fastify's inject method
- Configure validation, serialization, and error handling
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:
- rules/plugins.md - Plugin development and encapsulation
- rules/routes.md - Route organization and handlers
- rules/schemas.md - JSON Schema validation
- rules/error-handling.md - Error handling patterns
- rules/hooks.md - Hooks and request lifecycle
- rules/authentication.md - Authentication and authorization
- rules/testing.md - Testing with inject()
- rules/performance.md - Performance optimization
- rules/logging.md - Logging with Pino
- rules/typescript.md - TypeScript integration
- rules/decorators.md - Decorators and extensions
- rules/content-type.md - Content type parsing
- rules/serialization.md - Response serialization
- rules/cors-security.md - CORS and security headers
- rules/websockets.md - WebSocket support
- rules/database.md - Database integration patterns
- rules/configuration.md - Application configuration
- rules/deployment.md - Production deployment
- rules/http-proxy.md - HTTP proxying and reply.from()
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
More from mcollina/skills
typescript-magician
Designs complex generic types, refactors `any` types to strict alternatives, creates type guards and utility types, and resolves TypeScript compiler errors. Use when the user asks about TypeScript (TS) types, generics, type inference, type guards, removing `any` types, strict typing, type errors, `infer`, `extends`, conditional types, mapped types, template literal types, branded/opaque types, or utility types like `Partial`, `Record`, `ReturnType`, and `Awaited`.
1.2Knode
Provides domain-specific best practices for Node.js development with TypeScript, covering type stripping, async patterns, error handling, streams, modules, testing, performance, caching, logging, and more. Use when setting up Node.js projects with native TypeScript support, configuring type stripping (--experimental-strip-types), writing Node 22+ TypeScript without a build step, or when the user mentions 'native TypeScript in Node', 'strip types', 'Node 22 TypeScript', '.ts files without compilation', 'ts-node alternative', or needs guidance on error handling, graceful shutdown, flaky tests, profiling, or environment configuration in Node.js. Helps configure tsconfig.json for type stripping, set up package.json scripts, handle module resolution and import extensions, and apply robust patterns across the full Node.js stack.
1.2Knodejs-core
Debugs native module crashes, optimizes V8 performance, configures node-gyp builds, writes N-API/node-addon-api bindings, and diagnoses libuv event loop issues in Node.js. Use when working with C++ addons, native modules, binding.gyp, node-gyp errors, segfaults, memory leaks in native code, V8 optimization/deoptimization, libuv thread pool tuning, N-API or NAN bindings, build system failures, or any Node.js internals below the JavaScript layer.
772documentation
Creates, structures, and reviews technical documentation following the Diátaxis framework (tutorials, how-to guides, reference, and explanation pages). Use when a user needs to write or reorganize docs, structure a tutorial vs. a how-to guide, build reference docs or API documentation, create explanation pages, choose between Diátaxis documentation types, or improve existing documentation structure. Trigger terms include: documentation structure, Diátaxis, tutorials vs how-to guides, organize docs, user guide, reference docs, technical writing.
766octocat
Use this skill whenever the prompt contains any `github.com` URL, even if the user only pastes a link and gives no GitHub-specific keywords. Handles git and GitHub operations using the gh CLI. Triggers include any GitHub link to an issue, pull request, commit, compare page, Actions run, release, discussion, or repository. Covers creating and reviewing PRs, watching CI checks, interactive rebasing, branch cleanup, submodule management, and repository archaeology with git log/blame/bisect.
535skill-optimizer
Optimizes AI skills for activation, clarity, and cross-model reliability. Use when creating or editing skill packs, diagnosing weak skill uptake, reducing regressions, tuning instruction salience, improving examples, shrinking context cost, or setting benchmark/release gates for skills. Trigger terms: skill optimization, activation gap, benchmark skill, with/without skill delta, regression, context budget, prompt salience.
508