koa2中koa-router关于嵌套路由的使用:
var forums = new Router();
var posts = new Router();
posts.get('/', function (ctx, next) {...});
posts.get('/:pid', function (ctx, next) {...});
forums.use('/forums/:fid/posts', posts.routes(), posts.allowedMethods());
// responds to "/forums/123/posts" and "/forums/123/posts/123"
app.use(forums.routes());
我想请问以下posts.get('/:pid', function (ctx, next) {...});这行代码中/:pid是什么意思?
forums.use('/forums/:fid/posts', posts.routes(), posts.allowedMethods());
为什么同时可以响应// responds to "/forums/123/posts" and "/forums/123/posts/123",
这主路由又是怎么使用子路由的,谢谢!
posts.get('/:pid', function (ctx, next) {...});
/:pid,是指父路由forums(/forums/:fid/posts)后可以添加pid参数,eg:/forums/123/posts/123 数字123就是pid参数的值,可以通过ctx.params.pid访问到
post子路由有两个访问路径,一种是直接(posts.get('/',...)),
一种是传递pid参数(posts.get('/:pid',...))