---
id: sm-fastify-best-practices
name: "fastify-best-practices"
url: https://skills.yangsir.net/skill/sm-fastify-best-practices
author: mcollina
domain: ai-backend-engineering
tags: ["fastify", "nodejs", "web-frameworks", "api-development", "backend-performance"]
install_count: 21800
rating: 4.60 (55 reviews)
github: https://github.com/mcollina/skills
---

# fastify-best-practices

> 提供使用Fastify开发后端应用的最佳实践，涵盖插件实现、路由处理和架构指导，助您构建高性能服务。

**Stats**: 21,800 installs · 4.6/5 (55 reviews)

## Before / After 对比

### Fastify最佳实践：构建高性能后端服务的秘诀

## Readme

# fastify-best-practices

## 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](https://github.com/mcollina/skills/blob/HEAD/skills/fastify/rules/plugins.md) - Plugin development and encapsulation

- [rules/routes.md](https://github.com/mcollina/skills/blob/HEAD/skills/fastify/rules/routes.md) - Route organization and handlers

- [rules/schemas.md](https://github.com/mcollina/skills/blob/HEAD/skills/fastify/rules/schemas.md) - JSON Schema validation

- [rules/error-handling.md](https://github.com/mcollina/skills/blob/HEAD/skills/fastify/rules/error-handling.md) - Error handling patterns

- [rules/hooks.md](https://github.com/mcollina/skills/blob/HEAD/skills/fastify/rules/hooks.md) - Hooks and request lifecycle

- [rules/authentication.md](https://github.com/mcollina/skills/blob/HEAD/skills/fastify/rules/authentication.md) - Authentication and authorization

- [rules/testing.md](https://github.com/mcollina/skills/blob/HEAD/skills/fastify/rules/testing.md) - Testing with inject()

- [rules/performance.md](https://github.com/mcollina/skills/blob/HEAD/skills/fastify/rules/performance.md) - Performance optimization

- [rules/logging.md](https://github.com/mcollina/skills/blob/HEAD/skills/fastify/rules/logging.md) - Logging with Pino

- [rules/typescript.md](https://github.com/mcollina/skills/blob/HEAD/skills/fastify/rules/typescript.md) - TypeScript integration

- [rules/decorators.md](https://github.com/mcollina/skills/blob/HEAD/skills/fastify/rules/decorators.md) - Decorators and extensions

- [rules/content-type.md](https://github.com/mcollina/skills/blob/HEAD/skills/fastify/rules/content-type.md) - Content type parsing

- [rules/serialization.md](https://github.com/mcollina/skills/blob/HEAD/skills/fastify/rules/serialization.md) - Response serialization

- [rules/cors-security.md](https://github.com/mcollina/skills/blob/HEAD/skills/fastify/rules/cors-security.md) - CORS and security headers

- [rules/websockets.md](https://github.com/mcollina/skills/blob/HEAD/skills/fastify/rules/websockets.md) - WebSocket support

- [rules/database.md](https://github.com/mcollina/skills/blob/HEAD/skills/fastify/rules/database.md) - Database integration patterns

- [rules/configuration.md](https://github.com/mcollina/skills/blob/HEAD/skills/fastify/rules/configuration.md) - Application configuration

- [rules/deployment.md](https://github.com/mcollina/skills/blob/HEAD/skills/fastify/rules/deployment.md) - Production deployment

- [rules/http-proxy.md](https://github.com/mcollina/skills/blob/HEAD/skills/fastify/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

Weekly Installs737Repository[mcollina/skills](https://github.com/mcollina/skills)GitHub Stars1.5KFirst SeenJan 31, 2026Security Audits[Gen Agent Trust HubPass](/mcollina/skills/fastify-best-practices/security/agent-trust-hub)[SocketPass](/mcollina/skills/fastify-best-practices/security/socket)[SnykPass](/mcollina/skills/fastify-best-practices/security/snyk)Installed onopencode677codex673github-copilot670gemini-cli669cursor621kimi-cli619

---
*Source: https://skills.yangsir.net/skill/sm-fastify-best-practices*
*Markdown mirror: https://skills.yangsir.net/api/skill/sm-fastify-best-practices/markdown*