Closed lichaozhy closed 4 years ago
node.js version: 10.15.3
npm/yarn and version: 6.5.0
koa-router version: 8.0.0
koa-router
koa version: 2.7.4
koa
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);
Request: [GET] http://127.0.0.1/api/foo/test or http://127.0.0.1/api/bar/test
http://127.0.0.1/api/foo/test
http://127.0.0.1/api/bar/test
Response: 200 text/plain hello, world!
text/plain
Response: 404 text/plain Not found
But with string like,
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', childRouter.routes()); app.use(rootRouter.routes()).listen(80);
It works. It is correct when request get http://127.0.0.1/api/foo/test.
I watch the router instance stack rootRouter.stack in debugger. Here is the snapshot,
rootRouter.stack
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 '*' .
node.js version: 10.15.3
npm/yarn and version: 6.5.0
koa-router
version: 8.0.0koa
version: 2.7.4Code sample:
Expected Behavior:
Request: [GET]
http://127.0.0.1/api/foo/test
orhttp://127.0.0.1/api/bar/test
Response: 200
text/plain
hello, world!Actual Behavior:
Response: 404
text/plain
Not foundAdditional steps, HTTP request details, or to reproduce the behavior or a test case:
But with string like,
It works. It is correct when request get
http://127.0.0.1/api/foo/test
.Additional clues
I watch the router instance stack
rootRouter.stack
in debugger. Here is the snapshot,It looks very funny at the line marked by '*' .