koa下载
Koa 框架官网地址:https://www.koajs.com.cn/
使用npm命令下载 koa
$ npm install koa
使用代理路由 koa-router
$ npm install koa-router
由于项目使用 typescript 开发,所以还需要下载 koa、koa-router 注解
$ npm install @types/koa && npm install @types/koa-router
使用
在 src 目录下创建 app.ts 文件,该文件是整个项目的入口点。在 app.ts 文件引入 koa、koa-router 相关文件以及自定义配置文件。
若提示无法找到该文件的注解文件时,那么得下载注解文件,详细请查看 下载,若未使用 typescript 作为主语言则跳过。
import koa from "koa";
import koa_Router from "koa-router";
import serverConfig from "./config/server.config";
实例化 koa 与 koa-router
const app = new koa();
const KoaRouter = new koa_Router();
处理任意请求项,next 方法处理当前请求。若不使用 next 则请求无法处理,即使存在当前路径。
app.use(async (ctx: any, next: any) => { await next(); });
使用 koa-router 创建一个 get 请求
KoaRouter.get('/test/hello', async (ctx: any) => { ctx.body = 'hello world'; })
将 koa-router 交给 koa 代理
app.use(KoaRouter.routes());
将服务挂载到自定义端口, serverConfig 配置文件中设定的端口为 3000
app.listen(serverConfig.server.port, () => { console.log(`Server is started! Prot is ${serverConfig.server.port}`); });
配置文件
export default { server: { port: 3000 } };
使用 postman 请求得到 hello world
引入多路由文件
在项目中,为了避免代码杂乱、冲突,实现不同效果的代码都会分开保存在多个文件中,而路由文件与逻辑代码分开,逻辑文件在 controllers 文件夹,而路由在 routers 文件夹
在 routers 文件中夹存在这样一个文件 routers.ts ,用于存放通用的路由配置。
引入 koa-router
import koaRouter from "koa-router";
实例化 koa-router
const app = new koaRouter();
创建一个 get 路由
KoaRouter.get('/test/hello', async (ctx: any) => { ctx.body = 'hello world'; })
抛出 koa-router
export default app;
如何使用外部路由?
在 app.ts 中引入所需要的路由
import router from "./router/routers";
并将该路由交给 app.ts 文件中原有的 koa-router 路由代理,第一个参数可以为该路由添加上独有的路由例如: http://localhost: 3000/api/test/hello
注意: 路由引入顺序需要注意,例如有两个路由文件,一个是 /api 另一个是 /api/token,那么需要注意引入顺序,可能 /api 跑到 /api/token
KoaRouter.use("/api", router.routes());
印象笔记地址:https://app.yinxiang.com/fx/31791a0b-83c2-48e2-ab7d-1fb258dabbbe
Koa 框架官网地址:https://www.koajs.com.cn/
使用npm命令下载 koa
$ npm install koa
使用代理路由 koa-router
$ npm install koa-router
由于项目使用 typescript 开发,所以还需要下载 koa、koa-router 注解
$ npm install @types/koa && npm install @types/koa-router
使用
在 src 目录下创建 app.ts 文件,该文件是整个项目的入口点。在 app.ts 文件引入 koa、koa-router 相关文件以及自定义配置文件。
若提示无法找到该文件的注解文件时,那么得下载注解文件,详细请查看 下载,若未使用 typescript 作为主语言则跳过。
import koa from "koa";
import koa_Router from "koa-router";
import serverConfig from "./config/server.config";
实例化 koa 与 koa-router
const app = new koa();
const KoaRouter = new koa_Router();
处理任意请求项,next 方法处理当前请求。若不使用 next 则请求无法处理,即使存在当前路径。
app.use(async (ctx: any, next: any) => { await next(); });
使用 koa-router 创建一个 get 请求
KoaRouter.get('/test/hello', async (ctx: any) => { ctx.body = 'hello world'; })
将 koa-router 交给 koa 代理
app.use(KoaRouter.routes());
将服务挂载到自定义端口, serverConfig 配置文件中设定的端口为 3000
app.listen(serverConfig.server.port, () => { console.log(`Server is started! Prot is ${serverConfig.server.port}`); });
配置文件
export default { server: { port: 3000 } };
使用 postman 请求得到 hello world
引入多路由文件
在项目中,为了避免代码杂乱、冲突,实现不同效果的代码都会分开保存在多个文件中,而路由文件与逻辑代码分开,逻辑文件在 controllers 文件夹,而路由在 routers 文件夹
在 routers 文件中夹存在这样一个文件 routers.ts ,用于存放通用的路由配置。
引入 koa-router
import koaRouter from "koa-router";
实例化 koa-router
const app = new koaRouter();
创建一个 get 路由
KoaRouter.get('/test/hello', async (ctx: any) => { ctx.body = 'hello world'; })
抛出 koa-router
export default app;
如何使用外部路由?
在 app.ts 中引入所需要的路由
import router from "./router/routers";
并将该路由交给 app.ts 文件中原有的 koa-router 路由代理,第一个参数可以为该路由添加上独有的路由例如: http://localhost: 3000/api/test/hello
注意: 路由引入顺序需要注意,例如有两个路由文件,一个是 /api 另一个是 /api/token,那么需要注意引入顺序,可能 /api 跑到 /api/token
KoaRouter.use("/api", router.routes());
印象笔记地址:https://app.yinxiang.com/fx/31791a0b-83c2-48e2-ab7d-1fb258dabbbe