pregmatch pregmatch - 5 months ago 15
Node.js Question

Are this two routes same in nodejs?

Routes in Express:

/search/:company-name
/search/:category-name


I can see that first one is fired for both requests so they are same, but is there a way to solve this without involving for example:

/search/company/:company-name
/search/category/:category-name

Answer

Yes, they are the same.

The router just see a route that starts with search/ and ends with a wildcard. The only thing that change is the name you give to that wildcard, which doesn't matter for the router, it's still the same URL.

You can solve this by either changing the route, or you can parse the route argument (the wildcard) and do something different depending on its value.