安装
npm install koa-router --save
导入(并调用)
const router = require('koa-router')()
配置
router.get('/' , async ctx=>{
console.log(ctx.query)
ctx.body = "这是首页"
})
router.get('/new/:id' ,async ctx=>{
console.log(ctx.params)
ctx.body='这是新闻'
})
ctx.query : 获取URL 的传值返回对象 (‘http://localhost:3000/?id = 123’ ctx.query = { id: '123 ' } )。
--- 另外还有多种方法获取 查看api.
ctx.params : 获取动态路由 (‘http://localhost:3000/new/asd’ ctx.params = { id: 'asd ' } )
启动
app.use(router.routes()).use(router.allowedMethods());