重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
fastify-best-practise by thecodepace/fastify-skills
npx skills add https://github.com/thecodepace/fastify-skills --skill fastify-best-practise一套精心整理的规则和模式,用于构建生产就绪的 Fastify 应用程序。每条规则都包含错误和正确的示例及解释。
规则按主题组织在 rules/ 目录中。每条规则都遵循一致的格式,包含影响评级、错误/正确示例以及官方文档的引用。
| 规则 | 文件 | 影响 | 描述 |
|---|---|---|---|
| 配置 | configuration.md | 高 | 环境配置、日志记录器设置、安全选项和优雅关闭 |
| 创建服务器 | create-server.md | 低-中 | 使用 buildServer() 工厂函数实现可重用、可测试的服务器设置 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 创建插件 | create-plugin.md | 低-中 | 使用 fastify-plugin 将可重用功能封装在插件中 |
| 自动加载 | autoload.md | 高 | 使用 @fastify/autoload 从文件系统自动加载插件和路由 |
| 路由最佳实践 | route-best-practices.md | 中 | 使用插件/前缀组织路由,使用异步处理程序,完整的路由选项 |
| 模式验证 (Zod) | schema-validation-zod.md | 高 | 使用 Zod + fastify-type-provider-zod 进行类型安全的验证 |
| 封装 | encapsulation.md | 高 | 正确的作用域隔离以及何时使用 fastify-plugin |
| 错误处理 | error-handling.md | 高 | 自定义错误处理程序、@fastify/error、404 处理、结构化响应 |
| 钩子与生命周期 | hooks-lifecycle.md | 中 | 所有请求/响应和应用程序钩子:onRequest、preParsing、preValidation、preHandler、preSerialization、onError、onSend、onResponse、onTimeout、onRequestAbort、onReady、onListen、onClose、onRoute、onRegister |
| 日志记录 | logging.md | 高 | 内置 Pino 日志记录器、请求关联、数据脱敏、子日志记录器 |
| 身份验证 | authentication.md | 高 | 使用 @fastify/jwt 的 JWT 身份验证,使用 @fastify/auth 的多策略验证 |
| 测试 | testing.md | 高 | 使用 inject() 测试,buildServer 模式,vitest/node:test |
| TypeScript | typescript-integration.md | 中 | 类型提供者、模块增强、类型化装饰器 |
| 装饰器 | decorators.md | 中 | 使用 decorate / decorateRequest / decorateReply 扩展 Fastify 实例、请求和响应 |
| 内容类型解析器 | content-type-parser.md | 高 | 自定义内容类型解析器、请求体限制、多部分上传、通配符和正则表达式匹配 |
生成 Fastify 代码时,请阅读相关主题的规则文件并应用其中展示的模式。对于新项目,所有规则都相关。对于特定任务,仅加载所需内容:
create-server.md, configuration.md, autoload.md, encapsulation.md, typescript-integration.mdroute-best-practices.md, autoload.md, schema-validation-zod.mdcreate-plugin.md, autoload.md, encapsulation.mdconfiguration.mderror-handling.mdauthentication.md, hooks-lifecycle.md, encapsulation.mddecorators.md, typescript-integration.mdlogging.mdcontent-type-parser.mdtesting.md, create-server.md使用 @fastify/autoload,插件和路由会自动从其目录加载:
src/
plugins/ # 自动加载 — 共享插件 (使用 fastify-plugin)
db.ts
auth.ts
config.ts
routes/ # 自动加载 — 封装的路由插件 (不使用 fastify-plugin)
_hooks.ts # 全局路由钩子 (使用 autoHooks: true)
users/
index.ts # → /users
_hooks.ts # 仅适用于 /users 作用域的钩子
schema.ts
posts/
index.ts # → /posts
schema.ts
server.ts # 包含自动加载注册的 buildServer()
app.ts # 入口点 — 调用 buildServer() 和 listen()
test/
routes/
users.test.ts
posts.test.ts
helpers.ts # createTestServer() 辅助函数
应用这些最佳实践时,请提及遵循了哪些规则:
应用了 Fastify 最佳实践:
- 路由组织 : 按资源分组路由并使用前缀
- Zod 验证 : 具有类型推断的请求/响应模式
- 封装 : 共享插件使用
fastify-plugin,路由保持作用域- 错误处理 : 使用
@fastify/error的自定义错误处理程序
每周安装次数
47
代码仓库
GitHub 星标数
2
首次出现
2026年2月19日
安全审计
安装于
github-copilot45
cursor44
gemini-cli44
amp44
codex44
kimi-cli44
A curated set of rules and patterns for building production-ready Fastify applications. Each rule includes incorrect and correct examples with explanations.
The rules are organized by topic in the rules/ directory. Each rule follows a consistent format with impact rating, incorrect/correct examples, and references to official docs.
| Rule | File | Impact | Description |
|---|---|---|---|
| Configuration | configuration.md | HIGH | Environment config, logger setup, security options, and graceful shutdown |
| Create Server | create-server.md | LOW-MEDIUM | Use a buildServer() factory function for reusable, testable server setup |
| Create Plugin | create-plugin.md | LOW-MEDIUM | Encapsulate reusable functionality in plugins with fastify-plugin |
| Autoload | autoload.md | HIGH | Automatically load plugins and routes from the filesystem with @fastify/autoload |
| Route Best Practices | route-best-practices.md | MEDIUM | Organize routes with plugins/prefixes, use async handlers, full route options |
| Schema Validation (Zod) | schema-validation-zod.md | HIGH | Type-safe validation with Zod + fastify-type-provider-zod |
| Encapsulation | encapsulation.md | HIGH | Proper scope isolation and when to use fastify-plugin |
| Error Handling | error-handling.md | HIGH | Custom error handlers, @fastify/error, 404 handling, structured responses |
| Hooks & Lifecycle | hooks-lifecycle.md | MEDIUM | All request/reply and application hooks: onRequest, preParsing, preValidation, preHandler, preSerialization, onError, onSend, onResponse, onTimeout, onRequestAbort, onReady, onListen, onClose, onRoute, onRegister |
| Logging | logging.md | HIGH | Built-in Pino logger, request correlation, redaction, child loggers |
| Authentication | authentication.md | HIGH | JWT auth with @fastify/jwt, multi-strategy with @fastify/auth |
| Testing | testing.md | HIGH | Test with inject(), buildServer pattern, vitest/node:test |
| TypeScript | typescript-integration.md | MEDIUM | Type providers, module augmentation, typed decorators |
| Decorators | decorators.md | MEDIUM | Extend the Fastify instance, request, and reply with decorate / decorateRequest / decorateReply |
| Content Type Parser | content-type-parser.md | HIGH | Custom content type parsers, body limits, multipart uploads, catch-all and regex matching |
When generating Fastify code, read the relevant rule file(s) for the topic and apply the patterns shown. For a new project, all rules are relevant. For specific tasks, load only what's needed:
create-server.md, configuration.md, autoload.md, encapsulation.md, typescript-integration.mdroute-best-practices.md, autoload.md, schema-validation-zod.mdcreate-plugin.md, autoload.md, Using @fastify/autoload, plugins and routes are loaded automatically from their directories:
src/
plugins/ # Autoloaded — shared plugins (use fastify-plugin)
db.ts
auth.ts
config.ts
routes/ # Autoloaded — encapsulated route plugins (NO fastify-plugin)
_hooks.ts # Global route hooks (with autoHooks: true)
users/
index.ts # → /users
_hooks.ts # Hooks for /users scope only
schema.ts
posts/
index.ts # → /posts
schema.ts
server.ts # buildServer() with autoload registration
app.ts # Entry point — calls buildServer() and listen()
test/
routes/
users.test.ts
posts.test.ts
helpers.ts # createTestServer() helper
When applying these best practices, mention which rule(s) you followed:
Applied Fastify best practices:
- Route organization : Routes grouped by resource with prefixes
- Zod validation : Request/response schemas with type inference
- Encapsulation : Shared plugins use
fastify-plugin, routes stay scoped- Error handling : Custom error handler with
@fastify/error
Weekly Installs
47
Repository
GitHub Stars
2
First Seen
Feb 19, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
github-copilot45
cursor44
gemini-cli44
amp44
codex44
kimi-cli44
Lark Drive API 使用指南:飞书云文档、Wiki、表格 Token 处理与文件管理
48,300 周安装
Nx Plugins 插件库 - 查找安装 Nx 插件,提升开发效率与项目架构
793 周安装
McKinsey Consultant V3.1:基于渐进式披露与依赖感知的AI咨询技能,节省95%分析时间
794 周安装
Go 函数式编程库 samber/lo:类型安全、零依赖的集合处理工具
808 周安装
LangChain AI社交内容创作技能:自动化生成LinkedIn/Twitter图文帖
804 周安装
Go语言samber/oops结构化错误处理库:增强错误追踪与APM集成
812 周安装
Browserbase CLI:浏览器自动化工具,支持本地与远程模式,轻松应对反爬虫与验证码
818 周安装
encapsulation.mdconfiguration.mderror-handling.mdauthentication.md, hooks-lifecycle.md, encapsulation.mddecorators.md, typescript-integration.mdlogging.mdcontent-type-parser.mdtesting.md, create-server.md