项目作者: hacker0limbo

项目描述 :
typescript + express + mongodb 构建的 restful api 服务
高级语言: TypeScript
项目地址: git://github.com/hacker0limbo/express-restful.git
创建时间: 2019-12-24T09:49:18Z
项目社区:https://github.com/hacker0limbo/express-restful

开源协议:

下载


Express Restful

使用 typescript + express + mongodb 构建的 restful api 服务器, 包含测试用例

项目截图

测试用例如下:
demo

项目结构

  1. ├── src/ # 源代码目录
  2. ├── app.ts # 应用入口
  3. ├── authentication # auth service
  4. ├── controllers # 控制器
  5. ├── dto # 客户端发送的数据接口
  6. ├── exceptions # 异常
  7. ├── interfaces # 接口
  8. ├── middleware # 自定义中间件
  9. ├── models # orm 模型
  10. ├── operations # 数据操作 service
  11. ├── server.ts # 入口文件
  12. └── utils # 工具
  13. ├── tests/ # 测试目录
  14. ├── authentication.controller.test.ts
  15. ├── authentication.service.test.ts
  16. ├── dbHandler.ts
  17. └── post.controller.test.ts
  18. ├── package.json
  19. └── tsconfig.json

项目运行与测试

运行:

运行前需要修改 .env 下的环境变量

  1. $ git clone https://github.com/hacker0limbo/express-restful.git
  2. $ cd express-restful
  3. $ npm install
  4. $ npm run dev

测试:

  1. $ npm run test

API 接口

  1. /auth:
  2. POST /auth/login # 登录, 成功返回包含 token 的 cookie, 失败返回异常
  3. POST /auth/register # 注册, 成功返回对应用户信息, 失败返回异常
  4. POST /auth/logout # 登出, 删除客户端包含的含有 token 的 cookie
  5. /posts:
  6. GET /posts # 返回所有 posts, 不需要权限
  7. GET /posts/:id # 返回对应 id 的 post, 不需要权限
  8. POST /posts # 增加一个 post, 需要登录权限
  9. PUT /posts/:id # 更新一个 post, 需要登录权限
  10. DELETE /posts/:id # 删除一个 post, 需要登录权限
  11. /report/user:
  12. GET /report/user: # 所有用户的信息
  13. Exceptions 异常:
  14. DuplicateEmailException # 注册出现重复 email 抛出
  15. ExpiredAuthenticationToken # token 过期抛出
  16. HttpException # 异常基类
  17. InvalidAuthenticationToken # 客户端提供的错误 token 抛出
  18. InvalidCredentialsException # 登录密码出错抛出
  19. MissingAuthenticationToken # 未提供 token 抛出
  20. PostNotFoundException # 访问不存在的 post 抛出

参考