前言

大家好 我是歌谣 今天给大家带来node中关于koa路由模块的讲解

初始化项目

npm init -y

安装koa和路由

npm i koa
npm i koa-router

配置文件

{
  "name": "koa",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "koa": "^2.14.2"
  }
}

案例1

const Koa = require("koa")
const Router = require("koa-router")
const app = new Koa()
const router = new Router()
router.get("/list",(ctx,next)=>{
    ctx.body=["11111","22222"]
})
app.use(router.routes())

app.listen(3000)

运行结果

前端歌谣-第柒拾陆课-Koa路由讲解_html

案例2请求方法

const Koa = require("koa")
const Router = require("koa-router")
const app = new Koa()
const router = new Router()
router.post("/list",(ctx,next)=>{
    ctx.body={
        ok:"1",
        info:"添加成功"
    }
})
router.put("/list/:id",(ctx,next)=>{
    ctx.body={
        ok:"1",
        info:"修改成功"
    }
})
router.del("/list/:id",(ctx,next)=>{
    ctx.body={
        ok:"1",
        info:"删除成功"
    }
})
router.get("/list",(ctx,next)=>{
    ctx.body=["11111","22222"]
})
app.use(router.routes()).use(router.allowedMethods())

app.listen(3000)

案例2

indexRouter.js

const Koa = require("koa")
const app = new Koa()
const userRouter=require("./routes/user")
const listRouter=require("./routes/list")
const Router = require("koa-router")
const router = new Router()
//先注册路由级组件
router.use("/user",userRouter.routes(),userRouter.allowedMethods())
router.use("/list",listRouter.routes(),listRouter.allowedMethods())
app.use(router.routes()).use(router.allowedMethods())

app.listen(3000)

list.js

const Router = require("koa-router")
const router = new Router()
router.post("/",(ctx,next)=>{
    ctx.body={
        ok:"1",
        info:"添加成功"
    }
})
router.put("/:id",(ctx,next)=>{
    ctx.body={
        ok:"1",
        info:"修改成功"
    }
})
router.del("/:id",(ctx,next)=>{
    ctx.body={
        ok:"1",
        info:"删除成功"
    }
})
router.get("/",(ctx,next)=>{
    ctx.body=["11111","22222"]
})
module.exports=router

user.js

const Router = require("koa-router")
const router = new Router()
router.post("/",(ctx,next)=>{
    ctx.body={
        ok:"1",
        info:"添加成功"
    }
})
router.put("/:id",(ctx,next)=>{
    ctx.body={
        ok:"1",
        info:"修改成功"
    }
})
router.del("/:id",(ctx,next)=>{
    ctx.body={
        ok:"1",
        info:"删除成功"
    }
})
router.get("/",(ctx,next)=>{
    ctx.body=["11111","22222"]
})

module.exports=router

运行结果

前端歌谣-第柒拾陆课-Koa路由讲解_配置文件_02


前端歌谣-第柒拾陆课-Koa路由讲解_ci_03

案例产生页面

indexRouter.js

const Koa = require("koa")
const app = new Koa()
const router=require("./routes/index")
app.use(router.routes()).use(router.allowedMethods())

app.listen(3000)

home.js

const Router = require("koa-router")
const router = new Router()

router.get("/",(ctx,next)=>{
    ctx.body=`
    <html>我是主页</html>`
})
module.exports=router

index.js

const Router = require("koa-router")
const userRouter=require("./user.js")
const listRouter=require("./list.js")
const homeRouter=require("./home.js")
const router = new Router()
//统一添加
// router.prefix("/api")
//先注册路由级组件
router.use("/user",userRouter.routes(),userRouter.allowedMethods())
router.use("/list",listRouter.routes(),listRouter.allowedMethods())
router.use("/home",homeRouter.routes(),homeRouter.allowedMethods())
module.exports=router

运行结果

前端歌谣-第柒拾陆课-Koa路由讲解_ci_04