Grofit
Grofit

Reputation: 18445

Express 4 router implying parameter regex?

I have got a simple issue where I have 2 routes which do different things, one is:

blah\groups\:group_id

and then

blah\groups\count

Now the first returns a specific group, the latter returns the amount of groups the user has access to. Now the problem is that the first route is hit even when I use the 2nd route url. Which makes sense as it doesnt know that there is a different route for count. I was looking at doing regex to tell it to use group_id if it does not contain count however then I cannot use the router.param with it, so is there a way to tell express to use count first then if that isnt matched try the group_id one? or if not any way to keep the parameter name but attach some regex so it has the context of what to look for but retains the parameter name?

Upvotes: 0

Views: 167

Answers (1)

Ben Fortune
Ben Fortune

Reputation: 32117

Routes work like middleware and are executed in the order they are placed.

Having blah\groups\count before blah\groups\:group_id will make sure a match of count comes before :group_id.

Upvotes: 1

Related Questions