1.嵌套路由
var header = new router()
var save = new router()
save.get('/', async (ctx, next) => {
ctx.response.body = '<h1>Index Page</h1>'
})
save.get('/:id', async (ctx, next) => {
ctx.response.body = '<h1>Detail Page</h1>'
})
header.use('/blogs/123/save', save.routes(), save.allowedMethods())
// 调用路由中间件
app.use(header.routes())
2.路由前缀
var header = new router({
prefix: '/serve'
})
header.get('/:id', async (ctx, next) => {
ctx.response.body = '<h1>Index Page</h1>'
})
//匹配路由 localhost:3000/serve/3
和嵌套路由差不多。灵活使用
3.获取路由参数
ctx.params
//获取请求参数
ctx.request.query
ctx.request.queryString