介绍
该框架集成koa, 轻量级。可以重新组装符合自己的项目的框架
本框架是一个完整是项目,可以直接使用。
环境
- node
- typescript
- mysql
- redis
- vscode
源码
- 拉取代码
git clone https://github.com/klover2/koa-ts.git - 安装
yarn - vscode 安装代码格式
- Prettier - Code formatter
- TSLint
4. 配置好数据库 和redis 配置 在app/config中配置
5. 创建数据库blog_dev
6.
目录结构说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |-.vscode #vscode 配置文件 | |─launch.json #debug 调试 按 f5 启动 已经配置好了 | |-setting.json # vscode配置 |-app # 项目主体文件夹 | |-config # 配置文件夹 | | |-env # 环境配置文件夹 | | | |-api.ts # 正式服运行需要的配置 | | | |-dev.ts # 本地运行需要的配置 | | |-index.ts # 共同配置 同时通过process.env.NODE_ENV 判断环境 引入其他配置 | |-controller # 控制层 文件夹 | | |-user.controller.ts # 逻辑处理 | |-middleware # 中间件 文件夹 | | |-auth.ts # 统一错误处理 自定义ctx.result 挂载 | |-model # 模型 文件夹 | | |-index.ts # 数据库初始化同时创建表 | | |-user.model.ts # 用户模型 | |-routers # 路由 文件夹 | | |-index.ts # 路由文件入口 导入其他路由文件 | | |-user.route.ts # 自定义路由 | |-service # 服务层 文件夹 | |-utils #工具类 文件夹 | | |-interface # 接口统一放置 文件夹 | | |-index.ts # 常用方法 | | |-error_constructor.ts # 返回错误继承重写类 | | |-redis.ts # redis继承重写类 | |-dist # 编译后的文件 yarn run build | |-.gitignore # git 过滤文件 | |-.prettierignore # prettier 格式化代码过滤文件 | |-app.ts # 入口文件 | |-package.json # 依赖 | |-.prettierrc # prettier 代码格式化配置 | |-tsconfig.json # tsc 编译配置文件 | |-tslint.json # tslint 代码格式化配置 | |-yarn.lock # 锁定安装时的包的版本号 | |-README.md # 项目介绍文件 |
redis 的使用
在app.ts 已经注册了路由如下:
1 2 | import RedisServer from './app/utils/redis'; RedisServer('koa-ts_modules', config.redis_common); // 全局初始化redis |
在其他地方使用
1 2 3 4 5 6 7 | import RedisServer from '../utils/redis'; const redisServer = RedisServer('myblog-ts_modules'); ## 使用 let result = await redisServer._get('hh'); // _get 是重写的方法 也可以直接使用 let result = await redisServer.get('hh'); |
错误抛出
1 2 3 4 | import {<!-- --> CustomError } from '../utils/error_constructor'; # 第一个参数 描述错误 # 第二个参数 自定义错误要返回的其他 throw new CustomError('缺少参数', {<!-- --> msg: 'title、uid或者content缺少' }); |
返回
1 2 3 | 在auth.ts 中间件挂载了需要返回的参数格式 所以使用ctx.result['data'] = {<!-- -->} |
运行
本地运行
服务器上运行
模型使用
模型关联 在
1 2 3 4 5 6 7 8 9 | const {<!-- --> UserModel, ArticleModel } = db; UserModel.hasMany(ArticleModel, {<!-- --> foreignKey: 'uid', targetKey: 'id', as: 'artcleInfo' }); ArticleModel.belongsTo(UserModel, {<!-- --> foreignKey: 'uid', targetKey: 'id', as: 'userInfo', constraints: false, }); |
数据库增删改查
- 创建用户
1 2 3 4 5 6 7 | post http://localhost:3000/user/login {<!-- --> "name": "admin", "nickname": "admin", "pwd": "123" } |
- 创建文章
1 2 3 4 5 6 7 | post http://localhost:3000/article/create {<!-- --> "uid": 1, "title": "node+typescript+koa", "content": "node+typescript+koa" } |
- 修改文章
1 2 3 4 5 6 7 8 | patch http://localhost:3000/article/update {<!-- --> "uid": 1, "id": 1, "title": "node+typescript+koa", "content": "node+typescript+koa" } |
- 文章详情
1 | get http://localhost:3000/article/detail?id=1 |
- 文章列表
1 2 3 4 5 6 7 | post http://localhost:3000/article/list {<!-- --> "uid": 1, "limit": 10, "offset": 0 } |
- 文章删除
1 2 3 4 5 | delete http://localhost:3000/article/del {<!-- --> "id": 1 } |