'Express routes similar urls issue
i have an express routing file where i define all my routes . The problem is when i have tow routes with similar urls for example :
router.get('/:categoryId/', getProductsByCategory)
router.get('/:productId', getProduct)
and try to test with postman it always exacute the first route in order no matter which route i test so if the route with categoryId is above in the file it will execute getProductsByCategory even if i put the other url with productId and vide versa . I searched on internet but didn't find this issue anywhere. Thank you for your help !
Solution 1:[1]
@ghost Express routing follow up to down flow we can not define same routes name here you set same routes have to give different address try below routes and also follow this routes via postman
router.get('/category/:categoryId/', getProductsByCategory)
router.get('/product/:productId', getProduct)
Hope you get this point just let me know there were any other issue with express routing
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Smit Gajera |