关于 fastify-best-practices
面向后端开发者的 Fastify 技能,涵盖插件、路由、模式、测试和生产环境实践。
主要功能
- 01插件与路由设计
- 02模式验证与序列化
- 03错误处理与请求钩子
- 04身份验证与安全模式
- 05使用 inject() 测试
- 06性能与部署指导
适用场景
- 01组织 Fastify 后端结构
- 02添加基于模式的请求验证
- 03使用 inject() 测试路由
- 04为生产环境准备 Fastify 服务
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mcollina/ skills --skill fastify-best-practices安装后在 Claude Code 中输入/fastify-best-practices确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
tile.jsonrules/authentication.mdconfiguration.mdcontent-type.mdcors-security.mddatabase.mddecorators.mddeployment.mderror-handling.mdhooks.mdhttp-proxy.mdlogging.mdperformance.mdplugins.mdroutes.mdschemas.mdserialization.mdtesting.mdtypescript.mdwebsockets.md详细介绍
适用场景
在以下情况下使用此技能:
- 使用 Fastify 开发后端应用
- 实现 Fastify 插件和路由处理程序
- 获取有关 Fastify 架构和模式的指导
- 在 Fastify 中使用 TypeScript(剥离类型)
- 使用 Fastify 的 inject 方法实现测试
- 配置验证、序列化和错误处理
快速开始
一个精简且可运行的 Fastify 服务,可立即上手:
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? 从
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
使用方法
阅读各规则文件,了解详细说明和代码示例:
- rules/plugins.md - 插件开发和封装
- rules/routes.md - 路由组织和处理程序
- rules/schemas.md - JSON Schema 验证
- rules/error-handling.md - 错误处理模式
- rules/hooks.md - Hook 和请求生命周期
- 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()
核心原则
- 封装: Fastify 的插件系统可自动实现封装
- Schema 优先: 为验证和序列化定义 Schema
- 性能: Fastify 针对速度进行了优化;请正确使用其功能
- Async/await: 所有处理程序和 Hook 都支持异步函数
- 最少依赖: 优先使用 Fastify 内置功能和官方插件
评论
支持 Markdown