# koa-ts-router-base **Repository Path**: itchenliang/koa-ts-router-base ## Basic Information - **Project Name**: koa-ts-router-base - **Description**: 这是使用koa + koa-router + typescript + es6搭建的koa后台的base版本 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2021-08-06 - **Last Updated**: 2021-12-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: Koa, koa-router, TypeScript ## README ## koa-ts-router-base 该项目是针对 koa + typescript + koa-router的一个base版本,以后就可以在这个基础上创建项目,就不需要每次都去反复搭建项目。 ### 依赖 主要使用了`koa` + `koa-router` + `koa-bodyparser`来实现restful API接口的编写。 数据库以及数据库连接工具主要是使用了`mysql` + `sequelize`来操作。 ### 多路由自动加载 使用最原始的方式话,我们就需要经过如下的操作: ```ts const role = require('./routes/role') const user = require('./routes/user') ... app.use(role.routes()) app.use(role.allowedMethods()) app.use(user.routes()) app.use(user.allowedMethods()) ... ``` 如果有十个,那我们就要写次,导致很繁琐,所以接下来就看到我们动态加载多路由,我们只需要写一次就行了。 因为我们使用的是`ts`开发环境,所以无法使用或者说没摸索清楚`require-directory`,所以这里编写了一个中间件,并且使用`import()`来动态加载我们的多路由模块,实现代码如下: ```ts import KoaRouter from 'koa-router' import fs from 'fs' import path from 'path' const router = new KoaRouter() // 封装递归 // 如果在js环境的话就可以使用 require-directory 第三方库 function loadRoutes(filePath: string) { //将当前目录下 都读出来 const files = fs.readdirSync(filePath) // 过滤 files.filter(file => { //读取的时候会将当前目录的 index.js 也读取出来,这个不需要,如果拿到别的地方可以不加这个判断 if(filePath !== __dirname && file === 'index.ts'){ console.log("routes module must not be 'index.ts' " ) } return file !== 'index.ts' }).forEach(async file => { let newFilePath = path.join(filePath, file) // 是目录 if(fs.statSync(newFilePath).isDirectory()){ loadRoutes(newFilePath) // 递归 } else { // 方式一:使用require,但是这样的话,每个routes文件必须是module.exports = router // let route = require(newFilePath) // router.use(res.router.routes()) // router.use(res.router.allowedMethods()) // 因为该项目主要采用es6来写,所以我们还是用es的方式来导入 // 方式二:使用import() 和 export { router }, 每个routes文件就需要使用 export { router } 导出 // const res = await import(newFilePath) // router.use(res.router.routes()) // router.use(res.router.allowedMethods()) // 方式三:使用import() 和 export default router, 每个routes文件就需要使用 export default router 导出 const res = await import(newFilePath) router.use(res.default.routes()) router.use(res.default.allowedMethods()) } }) } //启动 loadRoutes(__dirname) module.exports = router ``` 这样在我们`app`中就可以采用下面的方式来使用我们的中间件: ```ts const routes = require('./routes') ... // routes app.use(routes.routes()) app.use(routes.allowedMethods()) ``` ### 如何使用 1. 首先使用`git clone`命令将代码克隆到本地 2. 修改`src/utils/seq.ts`里面的数据库相关配置 3. 然后使用`npm install`进行依赖的安装 4. 全局安装热加载工具`ts-node-dev` ```shell npm install ts-node-dev -g ``` 5. 启动项目 ```shell ts-node-dev src/app.ts ``` 等到终端出现如下效果,则表示项目启动成功 ![](https://gitee.com/itchenliang/img/raw/master/img/20210806094250.png) 6. 在浏览器输入`localhost:3001/api/role/list`,访问成功会出现如下如所示 ![](https://gitee.com/itchenliang/img/raw/master/img/20210806094646.png)