使用说明
Fastify面包片
零依赖的生产就绪Fastify模板 (官方Fastify套餐除外)。JSON解析速度比Fastify默认值快,验证速度比TypeBox快。仅使用Bun+Biome的现代工具。
为什么是这个模板?
- ⚡ 更快 -自定义JSON解析器,支持BigInt,优化验证器(比TypeBox快2-10倍)
- 🔒 安全 -原型污染预防、速率限制、CORS、头盔、漏洞扫描、代码中没有秘密
- 🎯 零存款 -只有官方的Fastify包、自定义验证器/不可变/实用程序
- 🛠️ 现代工具 -Bun运行时(测试/构建/开发)、Biome linter、原生节点测试API
- 📦 完成 -HTTP+CLI+集群+Docker+CI/CD+OpenAPI/Swagger
- 🤖 克劳德准备好了 -完整的CLAUDE.md,包含编码标准和工作流程
先决条件
- 包子 >= 1.3.0 (安装Bun)
就是这样!不需要其他全球工具。
快速开始
创建新项目
使用脚手架工具从此模板创建新项目:
# Using npx (after publishing to npm)
npx create-fastify-bun-starter my-app
# Or clone directly from GitHub
git clone https://github.com/eram/fastify-bun-starter.git my-app
cd my-app
bun install
使用模板
这 create-fastify-bun-starter 工具将:
- 克隆此存储库
- 删除git历史记录
- 定制
package.json包含您的项目详细信息 - 建立一个准备开发的清洁项目
只需运行:
npx create-fastify-bun-starter my-awesome-app
cd my-awesome-app
bun install
bun run dev
特性
演出
- 自定义JSON解析器 -BigInt支持、原型污染预防、SharedArrayBuffer处理
- 优化验证器 -比TypeBox快2-10倍,类似Zod-like API,零依赖
- 不可变对象 -具有运行时安全性的冻结数据结构
安全
- 内置保护 -限速、CORS、头盔、CSRF、原型污染防治
- 漏洞扫描 -Docker构建中的Grype扫描程序在关键CVE上失败
- 秘密侦查 -Biome linter在代码中捕获令牌/密码
- 80%代码覆盖率 -在CI中强制执行所需阈值
开发者体验
- Bun多功能一体机 -运行时、包管理器、测试运行器、bundler(没有单独的工具)
- 生物棉绒 -快速换行+格式化(无需ESLint/Pretier)
- 本地测试 -Node.js
node:testAPI,无Vitest/Jest(快速,无需配置) - TypeScript原生 -开发中没有构建步骤,源代码映射工作完美
- 克劳德优化 -CLAUDE.md,具有完整的AI配对编程上下文
生产就绪
- 聚类 -多核支持,具有优雅的重启和可配置的限制
- 码头工人 -基于Wolfi的多阶段构建,包括测试+扫描阶段
- OpenAPI/Swagger -自动生成的文档位于
/docs - CI/CD -语义版本控制、变更日志生成、基于git的发布
环境配置
该项目使用 NODE_ENV 为了确定运行时环境:
development(默认)-使用调试输出和env.print()进行本地开发和测试production-生产部署(在Dockerfile中设置,优化,日志记录最少)
环境文件
仅 .env.development 用git跟踪:
.env.development-开发/测试配置(以git跟踪,默认加载).env.local-本地覆盖(gitignored,可选).env-替代本地覆盖(gitignored,可选)
生产:直接在Dockerfile/部署平台中设置环境变量(NODE_ENV=生产环境)
环境变量
# Server Configuration
HOST=0.0.0.0 # Server host (default: 0.0.0.0)
PORT=3000 # Server port (default: 3000)
# Cluster Configuration
CLUSTER_WORKERS=4 # Number of workers (defaults to CPU count, max 32)
CLUSTER_RESTART_MAX=10 # Max restarts per window (default: 10)
CLUSTER_RESTART_WINDOW=60000 # Restart window in ms (default: 60000)
CLUSTER_SHUTDOWN_TIMEOUT=5000 # Shutdown timeout in ms (default: 5000)
以不同模式运行
# Development (default) - loads .env.development
bun run dev
# Testing - uses same .env.development
bun test
# Production - NODE_ENV=production set in Dockerfile
docker build -t my-app .
docker run -p 3000:3000 my-app
开发命令
# Run application with hot-reload
bun run dev
# Run HTTP server
npm run start
# or: bun src/app.ts
# Run with hot reload
npm run dev
# Run cluster mode (production)
npm run cluster
# MCP management CLI
npm run mcp list
npm run mcp add my-server --transport sse --url https://example.com/sse
# Run all tests (unit + integration)
bun test
# Run tests in watch mode
bun run test:watch
# Build Docker image
npm run build
# Create release
npm run release
# Publish to NPM (maintainers only)
npm run publish:npm
项目结构
src/ - Application source code
ci/ - Integration tests
script/ - Build and release scripts
.vscode/ - VSCode debug configurations
需求
- Bun>=1.0
- Docker(用于构建)
