Communitygithub.com

YepAPI/技能

Express.js 和 Fastify 的模式:中介軟體、路由組織、使用 Zod 進行驗證、錯誤處理、插件系統、請求生命週期、TypeScript 設定。

技能 是什麼?

此技能為使用 Express.js 和 Fastify 開發 Node.js Web 應用程式的開發者提供了一套全面的模式與規則。它強調組織良好的路由結構(按領域劃分)、使用 Zod 進行強型別的輸入驗證,以及統一的錯誤處理機制,以確保 API 的一致性與可靠性。此技能涵蓋了這兩個框架的核心概念,包括中介軟體順序、非同步錯誤處理、Fastify 的插件系統,以及與 TypeScript 的整合。透過遵循這些指南,AI 代理可以生成更易於維護、類型安全且強健的伺服器端程式碼,並避免常見的陷阱,如將業務邏輯放在路由處理程式中或忽略伺服器端驗證。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/express-fastify

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

Express & Fastify

規則

  • 按領域組織路由:routes/users.ts、routes/orders.ts — 每個資源一個路由器
  • 使用 Zod 驗證所有輸入:在處理前解析 req.body、req.params、req.query
  • 集中式錯誤處理:全域攔截中介軟體 (Express) 或 setErrorHandler (Fastify)
  • 回傳統一的錯誤格式:{ error: { code: string, message: string, details?: unknown } }
  • TypeScript:使用泛型為請求/回應指定型別 — Request<Params, ResBody, ReqBody, Query>
  • 中介軟體順序:CORS、body 解析器、認證、速率限制、路由、錯誤處理
  • 使用非同步路由處理器 — 搭配錯誤捕獲或使用 express-async-errors
  • Fastify:使用插件系統封裝路由和裝飾器 — fastify.register()
  • Fastify 結構描述:定義 JSON Schema 或使用 @fastify/type-provider-zod 進行驗證和序列化
  • 健康檢查:GET /health — 回傳 200 並包含資料庫/Redis 連線狀態
  • 優雅關機:監聽 SIGTERM,停止接受連線,完成進行中的請求

模式

// Express + Zod 驗證
import { Router } from "express";
import { z } from "zod";

const router = Router();
const CreateUserSchema = z.object({ email: z.string().email(), name: z.string().min(1) });

router.post("/users", async (req, res, next) => {
  try {
    const body = CreateUserSchema.parse(req.body);
    const user = await createUser(body);
    res.status(201).json(user);
  } catch (err) { next(err); }
});
// Fastify + Zod 型別提供者
import Fastify from "fastify";
import { serializerCompiler, validatorCompiler, ZodTypeProvider } from "fastify-type-provider-zod";

const app = Fastify().withTypeProvider<ZodTypeProvider>();
app.setValidatorCompiler(validatorCompiler);
app.setSerializerCompiler(serializerCompiler);

app.post("/users", { schema: { body: CreateUserSchema } }, async (req) => {
  return createUser(req.body); // 完全型別化
});

應避免

  • 將業務邏輯放在路由處理程式中 — 應提取到服務/領域層
  • 忘記 Express 的非同步錯誤處理 — 未處理的拒絕會導致程序崩潰
  • 在一個檔案中匯入所有路由 — 應使用 router.use() 或 Fastify 插件以實現模組化
  • 因為「前端會驗證」而跳過輸入驗證 — 永遠在伺服器端進行驗證

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Theme switching with CSS variables, system preference detection, next-themes, and persistence.

YepAPI/skills

Threaded comments, @mentions, reactions, moderation queue, optimistic UI, and cursor pagination.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

YepAPI/skills

Upload handling, resize/compress, CDN delivery, blur placeholders, and next/image patterns.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

相關技能