ZijianHe / koa-router

Router middleware for koa.
MIT License
4.85k stars 408 forks source link

Koa router matches empty strings on params #413

Open gotenxds opened 6 years ago

gotenxds commented 6 years ago

Hi!

The regexp koa uses to get params from the url is giving me some issues as its adding some empty strings to params

example: routh /api/vi/companies/:companyId url : /api/v1/companies/5a40b96828392b539d72edde will generate the regexp ^((?:.*))\/api\/v1((?:.*))\/companies\/((?:[^\/]+?))(?:\/(?=$))?(?=\/|$)

which will return the following results :

Full match  0-42    '/api/v1/companies/5a40b96828392b539d72edde'

Group 1.    0-0 ''

Group 2.    7-7 ''

Group 3.    18-42   '5a40b96828392b539d72edde'

The full match is being ignored by koa as it should be but group 1 and 2 are being used.

This can be tested here: https://regex101.com/r/RCgcC7/1

Edit: from a quick look at express's router it looks like they make sure to only add params that have a matching key, (i.e companyId) so '0' and '1' will not be added

jbielick commented 6 years ago

Are you using prefixes? Can you share more of the code you're using to generate those routes?