const Koa = require('koa');
const KoaRouter = require('@koa/router');
const app = new Koa();
const rootRouter = new KoaRouter({
prefix: '/api'
});
const childRouter = new KoaRouter();
childRouter.get('/test', ctx => {
ctx.body = 'hello, world!';
});
rootRouter.get('/', ctx => {
ctx.body = {
foo: 'bar'
};
}).use(['/foo', '/bar'], childRouter.routes());
app.use(rootRouter.routes()).listen(80);
It works. It is correct when request get http://127.0.0.1/api/foo/test
.
rootRouter
Router {opts: Object, methods: Array(7), params: Object, stack: Array(3)}
[[StableObjectId]]:3
methods:Array(7) ["HEAD", "OPTIONS", "GET", …]
opts:Object {prefix: "/api"}
params:Object {}
stack:Array(3) [Layer, Layer, Layer]
[[StableObjectId]]:4
length:3
__proto__:Array(0) [, …]
0:Layer {opts: Object, name: null, methods: Array(2), …}
1:Layer {opts: Object, name: null, methods: Array(2), …}
[[StableObjectId]]:6
methods:Array(2) ["HEAD", "GET"]
name:null
opts:Object {end: true, name: null, sensitive: false, …}
paramNames:Array(0) []
* path:"/api/bar/api/foo/test"
regexp:/^\/api\/bar\/api\/foo\/test(?:\/(?=$))?$/i {keys: Array(0), lastIndex: 0}
stack:Array(1) []
__proto__:Object {match: , params: , captures: , …}
2:Layer {opts: Object, name: null, methods: Array(2), …}
[[StableObjectId]]:6
methods:Array(2) ["HEAD", "GET"]
name:null
opts:Object {end: true, name: null, sensitive: false, …}
paramNames:Array(0) []
* path:"/api/bar/api/foo/test"
regexp:/^\/api\/bar\/api\/foo\/test(?:\/(?=$))?$/i {keys: Array(0), lastIndex: 0}
stack:Array(1) []
__proto__:Object {match: , params: , captures: , …}
__proto__:Object {acl: , bind: , checkout: , …}
It looks very funny at the line marked by '*' .