Keeping Routing Simple and Separated with Koa Router
Debugging an error when using koa-router module that multiple routes were invoked on a single HTTP request:
When making a request to get a list of users, it will match both routes:
Because in the second route
:id is optional, and since there is
yield next statement in the first route, the second route will be executed subsequently.
The cause of the bug is the design of the routing
GET /users/:id?. The correct routes are:
GET /users: Retrieve a list (array) of users.
GET /users/:id: Retrieve a single user object.
The question mark should not be appended to the end of
id parameter, which effectively marking the route to perform both duties. It’s better to keep them simple and keep them separated.