1.拆分的路由

  1. const router = require("koa-router")();
  2. router.get("/", async ctx => {
  3. ctx.body = "hello world"
  4. })
  5. router.get("/my", async ctx => {
  6. ctx.body = "my"
  7. })
  8. router.get("/friend", async ctx => {
  9. ctx.body = "f"
  10. })
  11. module.exports = router

2.项目的入口文件

  1. const koa = require("koa")
  2. const app = new koa();
  3. const router = require("./routers")
  4. app.use(router.routes());
  5. app.listen(4000)

一、拆分路由 - 图1