Search code examples
node.jskoa

Koa-router not nesting


const router2 = new KoaRouter();
router2.get(`/test`, async function(ctx, next) {
    ctx.body = 'api test';
});

const router = new KoaRouter();
router.get(`/1`, router2.routes(), router2.allowedMethods());
router.get(`/test`, async function(ctx, next) {
    ctx.body = 'test';
});

127.0.0.1:300/test works

127.0.0.1:300/1/test does not work

I can't see the problem?


Solution

  • When you are using nested routes you need to use router.use instead of router.get.