Open sujaykakkad opened 6 years ago
The problem is that, when nesting a router inside another one (via the router's .use()
method), the child route is automatically prefixed with the regular expression (.*)
, which will match every string of characters. This basically means that any structure of the form:
const parentRouter = new KoaRouter();
const childRouter = new KoaRouter();
childRouter.get('/route');
childRouter.get('/much/nest/such/route');
parentRouter.use(childRouter.routes()) // child router is prefixed with (.*) here
Will always result in the first route being called when using parentRouter, as the regex means that as long as it ends with /route
, it's a match.
I would really appreciate to have some context on this and understand why the route is prefixed with such a broad expression, as this makes some pieces of code potentially quite buggy.
I have written this piece of code
When I call
localhost:3000/countries
then it printscountries called
But it is conflicting with other routes alsolocalhost:3000/some_route_1/countries
printscountries called
localhost:3000/some_route_1/some_route_2/countries
printscountries called
localhost:3000/some_route_1/some_route_2/some_route_3/countries
printscountries called
and so onI guess it is adding it as suffix instead of prefix