{"id":25546996,"url":"https://github.com/fishjar/koa-rest-boilerplate","last_synced_at":"2026-02-11T21:30:16.446Z","repository":{"id":41779470,"uuid":"197146781","full_name":"fishjar/koa-rest-boilerplate","owner":"fishjar","description":"KOA REST boilerplate","archived":false,"fork":false,"pushed_at":"2022-12-22T11:43:50.000Z","size":533,"stargazers_count":0,"open_issues_count":11,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-05-28T23:02:43.717Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fishjar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-16T07:50:17.000Z","updated_at":"2024-05-28T23:02:43.717Z","dependencies_parsed_at":"2023-01-30T08:01:37.476Z","dependency_job_id":null,"html_url":"https://github.com/fishjar/koa-rest-boilerplate","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fishjar%2Fkoa-rest-boilerplate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fishjar%2Fkoa-rest-boilerplate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fishjar%2Fkoa-rest-boilerplate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fishjar%2Fkoa-rest-boilerplate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fishjar","download_url":"https://codeload.github.com/fishjar/koa-rest-boilerplate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239808526,"owners_count":19700455,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2025-02-20T09:19:08.268Z","updated_at":"2026-02-11T21:30:14.379Z","avatar_url":"https://github.com/fishjar.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# KOA REST boilerplate\n\n## 支持特性\n\n- 基于web框架[KOA](https://github.com/koajs/koa)，以及ORM框架[sequelize](https://github.com/sequelize/sequelize)，开箱即用\n- 简易登录及`JWT`验证、续签\n- 支持开发热重启，支持ES6语法，支持babel转码及编译压缩\n\n## 缺陷（待改进）\n\n- 可能存在SQL注入风险\n- 缺少文件上传、下载功能\n- 缺少单元测试\n\n## 目录结构\n\n```sh\n├── dist                      #输出目录\n├── docker-compose.yml\n├── Dockerfile\n├── package.json\n├── package-lock.json\n├── README.md\n├── src                       #源码目录\n│   ├── app.js                #koa配置\n│   ├── config                #配置目录\n│   │   ├── config.development.js\n│   │   ├── config.production.js\n│   │   ├── config.test.js\n│   │   └── index.js\n│   ├── db                    #数据库配置及文件目录\n│   │   └── index.js\n│   ├── handler               #路由handler目录\n│   │   ├── account.js\n│   │   ├── foo.js\n│   │   └── index.js\n│   ├── log                   #日志目录\n│   ├── middleware            #中间件目录\n│   │   ├── errorHandler.js   #全局错误捕获\n│   │   ├── jwtAuth.js        #jwt认证\n│   │   ├── jwtRolling.js     #jwt刷新\n│   │   ├── koaBody.js        #请求body处理\n│   │   └── reqBodyLog.js     #请求日志\n│   ├── model                 #模型目录\n│   │   ├── foo.js\n│   │   ├── index.js\n│   │   └── user.js\n│   ├── router                #路由配置\n│   │   └── index.js\n│   ├── server.js             #启动文件\n│   └── utils                 #工具包\n│       ├── api.js            #api封装\n│       ├── index.js          #工具函数\n│       ├── jwt.js            #jwt封装\n│       ├── logger.js         #日志函数配置\n│       ├── request.js        #请求函数封装\n│       └── sign.js           #加密封装\n└── yarn.lock\n```\n\n## 依赖列表\n\n```js\n\"dependencies\": {\n  \"@koa/cors\": \"^2.2.2\", // 跨域请求配置及处理\n  \"cross-env\": \"^5.2.0\", // 跨系统的环境设置\n  \"jsonwebtoken\": \"^8.4.0\", // JWT 认证插件\n  \"koa\": \"^2.6.1\", // 核心包\n  \"koa-body\": \"^4.2.1\", // body解析\n  \"koa-compress\": \"^3.0.0\", // gzip压缩\n  \"koa-jwt\": \"^3.5.1\", // JWT 验证\n  \"koa-logger\": \"^3.2.0\", // 日志中间件\n  \"koa-qs\": \"^2.0.0\", // querystring处理\n  \"koa-router\": \"^7.4.0\", // 路由插件\n  \"mysql2\": \"^1.6.4\", // mysql支持\n  \"request\": \"^2.88.0\", // http请求插件\n  \"request-promise\": \"^4.2.2\",\n  \"sequelize\": \"^4.41.1\", // ORM插件\n  \"strip-ansi\": \"^5.0.0\", // 输出纯净日志文本\n  \"winston\": \"^3.1.0\", // 日志框架\n  \"winston-daily-rotate-file\": \"^3.5.1\" // 日志按日期分割\n},\n\"devDependencies\": {\n  \"babel-cli\": \"^6.26.0\", // ES6支持\n  \"babel-plugin-transform-object-rest-spread\": \"^6.26.0\",\n  \"babel-plugin-transform-runtime\": \"^6.23.0\",\n  \"babel-preset-env\": \"^1.7.0\",\n  \"babel-preset-minify\": \"^0.5.0\", // 压缩\n  \"nodemon\": \"^1.18.6\", // 热重启\n  \"rimraf\": \"^2.6.2\", // 清空文件夹\n  \"sqlite3\": \"^4.0.3\" // sqlite支持\n},\n```\n\n## 使用指引\n\n```sh\n# 创建并进入目录\nmkdir koa-rest-boilerplate \u0026\u0026 cd \"$_\"\n\n# 克隆项目\ngit clone https://github.com/fishjar/koa-rest-boilerplate.git .\n\n# 安装依赖\nyarn\n\n# 开发\nyarn dev\n\n# 编译（清空dist文件夹+转码+压缩）\nyarn build\n\n# 启动编译后代码\nyarn start\n\n# 开发时，如有需要，运行下列命令启动一个mysql数据库服务\nsudo docker-compose -f ./src/db/docker-compose.mysql.yml up -d\n\n# 简易部署\nsudo docker-compose up -d\n```\n\n## 模型验证参数参考\n\n```js\nconst ValidateMe = sequelize.define(\"foo\", {\n  foo: {\n    type: Sequelize.STRING,\n    validate: {\n      is: [\"^[a-z]+$\", \"i\"], // 只允许字母\n      is: /^[a-z]+$/i, // 与上一个示例相同,使用了真正的正则表达式\n      not: [\"[a-z]\", \"i\"], // 不允许字母\n      isEmail: true, // 检查邮件格式 (foo@bar.com)\n      isUrl: true, // 检查连接格式 (http://foo.com)\n      isIP: true, // 检查 IPv4 (129.89.23.1) 或 IPv6 格式\n      isIPv4: true, // 检查 IPv4 (129.89.23.1) 格式\n      isIPv6: true, // 检查 IPv6 格式\n      isAlpha: true, // 只允许字母\n      isAlphanumeric: true, // 只允许使用字母数字\n      isNumeric: true, // 只允许数字\n      isInt: true, // 检查是否为有效整数\n      isFloat: true, // 检查是否为有效浮点数\n      isDecimal: true, // 检查是否为任意数字\n      isLowercase: true, // 检查是否为小写\n      isUppercase: true, // 检查是否为大写\n      notNull: true, // 不允许为空\n      isNull: true, // 只允许为空\n      notEmpty: true, // 不允许空字符串\n      equals: \"specific value\", // 只允许一个特定值\n      contains: \"foo\", // 检查是否包含特定的子字符串\n      notIn: [[\"foo\", \"bar\"]], // 检查是否值不是其中之一\n      isIn: [[\"foo\", \"bar\"]], // 检查是否值是其中之一\n      notContains: \"bar\", // 不允许包含特定的子字符串\n      len: [2, 10], // 只允许长度在2到10之间的值\n      isUUID: 4, // 只允许uuids\n      isDate: true, // 只允许日期字符串\n      isAfter: \"2011-11-05\", // 只允许在特定日期之后的日期字符串\n      isBefore: \"2011-11-05\", // 只允许在特定日期之前的日期字符串\n      max: 23, // 只允许值 \u003c= 23\n      min: 23, // 只允许值 \u003e= 23\n      isCreditCard: true, // 检查有效的信用卡号码\n\n      // 也可以自定义验证:\n      isEven(value) {\n        if (parseInt(value) % 2 != 0) {\n          throw new Error(\"Only even values are allowed!\");\n          // 我们也在模型的上下文中，所以如果它存在的话,\n          // this.otherField会得到otherField的值。\n        }\n      }\n    }\n  }\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffishjar%2Fkoa-rest-boilerplate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffishjar%2Fkoa-rest-boilerplate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffishjar%2Fkoa-rest-boilerplate/lists"}