fastify-best-practices

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

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
当你需要以下操作时使用本技能:
  • 使用Fastify开发后端应用
  • 实现Fastify插件与路由处理器
  • 获取Fastify架构与模式相关指导
  • 在Fastify中使用TypeScript(strip types)
  • 使用Fastify的inject方法实现测试
  • 配置验证、序列化与错误处理

Quick Start

快速开始

A minimal, runnable Fastify server to get started immediately:
ts
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()
一个极简可运行的Fastify服务器,助你快速上手:
ts
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
  • 初次接触Fastify?
    plugins.md
    routes.md
    schemas.md
    开始
  • 添加身份验证:
    plugins.md
    hooks.md
    authentication.md
  • 性能优化:
    schemas.md
    serialization.md
    performance.md
  • 设置测试:
    routes.md
    testing.md
  • 上线生产环境:
    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()
阅读单个规则文件获取详细说明与代码示例:
  • rules/plugins.md - 插件开发与封装
  • rules/routes.md - 路由组织与处理器
  • rules/schemas.md - JSON Schema验证
  • rules/error-handling.md - 错误处理模式
  • rules/hooks.md - 钩子与请求生命周期
  • rules/authentication.md - 身份验证与授权
  • rules/testing.md - 使用inject()进行测试
  • rules/performance.md - 性能优化
  • rules/logging.md - 基于Pino的日志记录
  • rules/typescript.md - TypeScript集成
  • rules/decorators.md - 装饰器与扩展
  • rules/content-type.md - 内容类型解析
  • rules/serialization.md - 响应序列化
  • rules/cors-security.md - CORS与安全标头
  • rules/websockets.md - WebSocket支持
  • rules/database.md - 数据库集成模式
  • rules/configuration.md - 应用配置
  • rules/deployment.md - 生产环境部署
  • rules/http-proxy.md - HTTP代理与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
  • 封装性:Fastify的插件系统提供自动封装功能
  • 优先使用Schema:定义Schema用于验证与序列化
  • 性能:Fastify针对速度进行了优化;请正确使用其功能
  • Async/await:所有处理器与钩子均支持异步函数
  • 最小依赖:优先使用Fastify的内置功能与官方插件