RESTful API service for surmon.me blog, powered by nestjs, required mongoDB & Redis.
适用于 surmon.me 的 RESTful API 服务;基于 nestjs (nodejs); 需安装 mongoDB 和 Redis 方可完整运行。
v3.x 使用 nestjs 进行重构,之前的 nodejs 版本在 此分支。
其他相关项目:
更新记录:CHANGELOG.md
接口文档:API_DOC.md
HTTP 状态码(详见 errors )
400
请求的业务被拒绝401
鉴权失败403
权限不足/请求参数需要更高的权限404
资源不存在405
无此方法500
服务器挂了200
正常201
POST 正常数据特征码(详见 http.interface.ts )
status
:
success
:正常error
:异常message
:永远返回(由 http.decorator 装饰)error
:一般会返回错误发生节点的 error;在 status
为 error
的时候必须返回,方便调试debug
:开发模式下为发生错误的堆栈,生产模式不返回result
:在 status
为 success
的时候必须返回
{ pagination: {...}, data: {..} }
{ title: '', content: ... }
通用
extend
为通用扩展(模型在此)各表重要字段
_id
:mongodb 生成的 id,一般用于后台执行 CRUD 操作id
:插件生成的自增数字 id,类似 mysql 中的 id,具有唯一性,用于前台获取数据pid
:父级 id,用于建立数据表关系,与 id 字段映射数据组成的几种可能
入口
main.ts
:引入配置,启动主程序,引入各种全局服务app.module.ts
:主程序根模块,负责各业务模块的聚合app.controller.ts
:主程序根控制器app.config.ts
:主程序配置,数据库、程序、第三方,一切可配置项app.environment.ts:
全局环境变量请求处理流程
request
:收到请求middleware
:中间件过滤(跨域、来源校验等处理)guard
:守卫过滤(鉴权)interceptor:before
:数据流拦截器(本应用为空,即:无处理)pipe
:参数提取(校验)器controller
:业务控制器service
:业务服务interceptor:after
:数据流拦截器(格式化数据、错误)filter
:捕获以上所有流程中出现的异常,如果任何一个环节抛出异常,则返回错误鉴权处理流程
guard
:守卫 分析请求guard.canActivate
:继承处理JwtStrategy.validate
:调用 鉴权服务
guard.handleRequest
:根据鉴权服务返回的结果作判断处理,通行或拦截
鉴权级别
参数校验逻辑(代码见 query-params.decorator.ts )
错误过滤器(代码见 error.filter.ts )
拦截器 interceptors
装饰器扩展 decorators
守卫 guards
中间件 middlewares
管道 pipes
业务模块 modules
核心辅助模块 processors
# 安装
$ yarn
# 开发
$ yarn start:dev
# 测试
$ yarn lint
$ yarn test
$ yarn test:e2e
$ yarn test:cov
$ yarn test:watch
# 构建
$ yarn build
# 生产环境运行
$ yarn start:prod
# 更新 GEO IP 库数据
$ yarn update-geo-db
Rule:
any PR open
→ CI:Build test
master PR closed & merged
→ CI:Deploy to server
Example:
local:develop
→ remote:develop
→ CI:Build test
remote:develop/master
→ remote:master → merged
→ CI:Deploy to server