Skills2026年4月11日·1 分钟阅读

Fastify — Fast & Low Overhead Web Framework for Node.js

Fastify is a fast and low overhead web framework for Node.js, focused on providing the best developer experience with least overhead and a powerful plugin architecture. Schema-driven with JSON Schema validation built in.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
step-1.md
直接安装命令
npx -y tokrepo@latest install 267271ae-355f-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

TL;DR
Fastify provides schema-driven validation and fast JSON serialization for Node.js APIs.
§01

What it is

Fastify is a web framework for Node.js focused on speed and low overhead. It uses JSON Schema for request validation and response serialization, achieving throughput that matches or exceeds other Node.js frameworks. The plugin architecture keeps code modular and testable. Built-in TypeScript support and automatic Swagger generation round out the developer experience.

This tool is for Node.js developers who need better performance than Express without sacrificing developer experience. It is especially suited for API-heavy applications where validation and serialization matter.

§02

How it saves time or tokens

Fastify's schema-driven approach validates inputs and serializes outputs automatically. Define a JSON Schema for your route, and Fastify rejects invalid requests before your handler runs, generates OpenAPI documentation, and serializes responses faster than JSON.stringify. The plugin system encourages reusable modules.

§03

How to use

  1. Install Fastify.
  2. Define routes with JSON Schema validation.
  3. Register plugins for functionality.
  4. Start the server.
# Create project
mkdir my-api && cd my-api
npm init -y
npm install fastify
§04

Example

const fastify = require('fastify')({ logger: true })

// Route with schema validation
fastify.post('/users', {
  schema: {
    body: {
      type: 'object',
      required: ['name', 'email'],
      properties: {
        name: { type: 'string', minLength: 1 },
        email: { type: 'string', format: 'email' }
      }
    },
    response: {
      201: {
        type: 'object',
        properties: {
          id: { type: 'string' },
          name: { type: 'string' },
          email: { type: 'string' }
        }
      }
    }
  }
}, async (request, reply) => {
  const user = {
    id: crypto.randomUUID(),
    ...request.body
  }
  return reply.code(201).send(user)
})

fastify.listen({ port: 3000 })

Invalid requests are rejected automatically with descriptive error messages.

§05

Related on TokRepo

§06

Common pitfalls

  • Fastify's plugin encapsulation can be confusing. Plugins registered in one scope are not available in another without proper decoration.
  • JSON Schema validation is strict by default. Properties not in the schema are stripped from requests. Set additionalProperties: true if you need them.
  • Response serialization requires accurate response schemas. Mismatched schemas cause serialization errors or data loss.
  • Fastify uses async/await natively. Callback-style code from Express needs refactoring.
  • The decorator pattern for dependency injection has a learning curve. Understand fastify.decorate() before building complex applications.
  • Review the official documentation before deploying to production to ensure compatibility with your specific environment and requirements.
  • Start with default settings and customize incrementally. Changing too many configuration options at once makes debugging harder.

常见问题

How fast is Fastify compared to Express?+

Fastify is typically 2-3x faster than Express in benchmarks for JSON response handling. The speed comes from fast JSON serialization via fast-json-stringify and efficient routing via find-my-way.

Does Fastify support TypeScript?+

Yes. Fastify has built-in TypeScript support with type-safe route definitions, plugin types, and schema inference. The types are maintained as part of the core package.

Can Fastify generate OpenAPI documentation?+

Yes. The @fastify/swagger plugin generates OpenAPI 3.0 documentation from your route schemas automatically. Add @fastify/swagger-ui for a browsable Swagger interface.

Is Fastify compatible with Express middleware?+

Not directly, but the @fastify/express plugin provides a compatibility layer. It lets you use Express middleware within Fastify, easing migration from Express.

How does the plugin system work?+

Fastify plugins are functions that receive the Fastify instance and options. They can add routes, decorators, hooks, and register sub-plugins. Encapsulation ensures plugins do not leak state into other parts of the application.

引用来源 (3)

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产