I'm trying to create a route that will act the same for different prefixes: koa-router with multiple prefixes for the same set of routes:
/player/:id
/players/:id <- Same as above
/player/search
/players/search <- Same as above
Both of those are exactly the same method, given twice for convenience of the users.
In Express, obtaining this is easy, as ?
will make the s
optional:
router.use('/players?', ...);
In Koa, this isn't valid.
I have tried creating a sub-router with the two options:
const router = new Router();
router.get('/:id', ...);
// And then:
const player = new Router();
player.use('/player', router.routes());
player.use('/players', router.routes());
But this will actually register:
players/player/:id
Replacing .use
with .get
will ignore router.routes()
and register /player
without :id
.
Is there a way to achieve a router that accepts different routes without creating two separate routers?