Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if optional term in the route, params are shifted in req.params (ROR) #2491

Closed
doom-fr opened this issue Jan 9, 2015 · 3 comments
Closed
Assignees
Milestone

Comments

@doom-fr
Copy link

doom-fr commented Jan 9, 2015

I am using express (4.10.6) with a route that has a optional term.

app.get("/truc(muche)?/bidule/:machin/:chose",my_function);

The route work great as /truc/bidule/head/foot and /trucmuche/bidule/head/foot is well routed to my_function.

But there's a problem because when entering the function,the req.params is :

for /truc :      { '0': 'foot', machin: undefined, chose: 'head' }
for /trucmuche : { '0': 'foot', machin: 'muche', chose: 'head' }

instead of what seems to be expected :

for /truc :      { '0': undefined, machin: 'head', chose: 'foot' }
for /trucmuche : { '0': 'muche', machin: 'head', chose: 'foot' }

Strange !

Thanks for your help.

Doom.

@dougwilson
Copy link
Contributor

It's unfortunate it's a bug in the old version of path-to-regexp we use, and since it's so old, it may not get fixed. In the meantime, you can always use the path /:first(truc(muche)?)/bidule/:machin/:chose (i.e. give the first path segment a name and the regexp to metch for it).

@dougwilson dougwilson self-assigned this Jan 9, 2015
@dougwilson
Copy link
Contributor

Basically the limitation of the path-to-regexp we are using is that you cannot have your own capture groups before named parameters in the path.

@dougwilson
Copy link
Contributor

Good news! This is now fixed in path-to-regexp 0.1.5, which is Express 4.x compatible! Bad news is we cannot yet moved to 0.1.5 until the following bug is fixed: pillarjs/path-to-regexp#55

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants