{"id":28504275,"url":"https://github.com/slash214/fastify-v4","last_synced_at":"2026-04-20T10:33:03.881Z","repository":{"id":294902510,"uuid":"980388811","full_name":"Slash214/fastify-v4","owner":"Slash214","description":"“A secure, extensible Fastify v4 boilerplate for building Node.js REST APIs with MySQL (Sequelize), featuring dynamic module loading, CORS, Helmet security, rate-limiting, unified responses, and prettified logging.”","archived":false,"fork":false,"pushed_at":"2025-05-22T15:28:39.000Z","size":39,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-10T14:21:19.452Z","etag":null,"topics":["api","boilerplate","fastify","nodejs","template"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Slash214.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-05-09T03:43:58.000Z","updated_at":"2025-05-22T15:28:42.000Z","dependencies_parsed_at":"2025-05-22T16:57:51.715Z","dependency_job_id":"b86df9a6-5138-415b-876c-9dafc7ba501b","html_url":"https://github.com/Slash214/fastify-v4","commit_stats":null,"previous_names":["slash214/fastify-v4"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Slash214/fastify-v4","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Slash214%2Ffastify-v4","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Slash214%2Ffastify-v4/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Slash214%2Ffastify-v4/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Slash214%2Ffastify-v4/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Slash214","download_url":"https://codeload.github.com/Slash214/fastify-v4/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Slash214%2Ffastify-v4/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32043053,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-20T00:18:06.643Z","status":"online","status_checked_at":"2026-04-20T02:00:06.527Z","response_time":94,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","boilerplate","fastify","nodejs","template"],"created_at":"2025-06-08T18:04:49.105Z","updated_at":"2026-04-20T10:33:03.863Z","avatar_url":"https://github.com/Slash214.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Fastify V4 Boilerplate\n\n这是一个基于 **Fastify v4** + **Sequelize(MySQL)** 的后台服务模板，适用于个人项目和小型企业后台 API 开发。模板已经集成：\n\n-   Fastify v4 核心\n-   MySQL 数据库连接 (通过 Sequelize ORM)\n-   动态加载模型与路由\n-   JWT 身份验证 (通过 @fastify/jwt)\n-   Swagger UI + 自动文档 (@fastify/swagger)\n-   CORS 支持 (@fastify/cors)\n-   Helmet HTTP 头部安全配置 (@fastify/helmet)\n-   请求限流 (@fastify/rate-limit)\n-   全局符合格式的 JSON 响应\n-   全局错误处理\n-   404 路由处理\n-   日志美化 (pino-pretty)\n-   环境变量管理 (.env)\n\n---\n\n## 安装与启动\n\n1. 克隆项目并安装依赖：\n\n    ```bash\n    git clone \u003cyour-repo-url\u003e\n    cd fastify-v4\n    npm install\n    ```\n\n2. 创建数据库：\n\n    ```sql\n    CREATE DATABASE IF NOT EXISTS your_database\n      CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\n    ```\n\n3. 配置环境变量：\n\n    ```dotenv\n    NODE_ENV=development\n    PORT=3120\n\n    DB_HOST=localhost\n    DB_PORT=3306\n    DB_NAME=your_database\n    DB_USER=your_user\n    DB_PASS=your_password\n\n    JWT_SECRET=your-secure-secret\n\n    CORS_ORIGIN=https://your-frontend.com  # 允许跨域域名\n    ```\n\n4. 本地启动：\n\n    ```bash\n    npm run dev\n    ```\n\n5. 生产环境启动：\n\n    ```bash\n    npm start\n    ```\n\n6. 访问 API 文档：\n\n    ```url\n    http://localhost:3120/docs\n    ```\n\n---\n\n## 插件配置示例\n\n### 安全头 (Helmet)\n\n```js\n// plugins/02-helmet.js\nconst fp = require('fastify-plugin')\nconst helmet = require('@fastify/helmet')\nconst { security } = require('../config')\n\nmodule.exports = fp(async (fastify) =\u003e {\n    await fastify.register(helmet, security)\n})\n```\n\n### 限流 (Rate Limit)\n\n```js\n// plugins/04-rate-limit.js\nconst fp = require('fastify-plugin')\nconst rateLimit = require('@fastify/rate-limit')\nconst { rateLimit: config } = require('../config')\n\nmodule.exports = fp(async (fastify) =\u003e {\n    await fastify.register(rateLimit, config)\n})\n```\n\n### CORS\n\n```js\n// plugins/03-cors.js\nconst fp = require('fastify-plugin')\nconst cors = require('@fastify/cors')\nconst { cors: corsConfig } = require('../config')\n\nmodule.exports = fp(async (fastify) =\u003e {\n    await fastify.register(cors, corsConfig)\n})\n```\n\n---\n\n## 项目目录\n\n```\nsrc/\n├── index.js              // 应用入口\n├── config/               // 配置文件 (.env -\u003e config)\n├── plugins/              // Fastify 插件\n│   ├── 01-sequelize.js   // ORM 数据库连接\n│   ├── 02-helmet.js      // 安全头插件\n│   ├── 03-cors.js        // 跨域配置\n│   ├── 04-rate-limit.js  // 请求限流\n│   ├── 05-schema-loader.js\n│   ├── 06-jwt.js         // JWT 身份验证\n├── controllers/          // 控制器层\n├── services/             // 业务逻辑\n├── routes/               // 路由分组\n├── models/               // Sequelize 模型\n├── schemas/              // JSON Schema 验证\n├── utils/                // 工具函数\n└── response.js           // 全局响应格式\n```\n\n---\n\n## 开发指南\n\n1. 新增模型：`/models/*.model.js` 中定义数据库模型\n2. 新增路由：`/routes/*.routes.js` 中定义接口定义\n3. 统一响应：所有 `reply.send(data)` 都会被格式化为 `{ code, message, data }`\n4. JWT 验证：在需要验证的路由中使用 `preHandler: [fastify.authenticate]`\n5. Swagger 文档：默认启用 `/docs` 路由，支持 tags + 接口模型\n\n---\n\n## 后续可选扩展\n-   配置化 RBAC (角色執行权限)\n-   OAuth2/三方登录组件\n-   使用 Sequelize 迁移 CLI + Umzug 管理数据表\n-   Jest 自动化接口测试\n-   Docker 化部署 \u0026 CI/CD\n\n---\n\n欢迎 Star 以支持我们，并可在此基础上自由扩展！\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslash214%2Ffastify-v4","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fslash214%2Ffastify-v4","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fslash214%2Ffastify-v4/lists"}