I have below kind of route in my project
router.get('/new', async function(req,res){
});
router.get('/:id', async function (req, res) {
});
when I invoked /new
route from post-man, both the routes are executing. first the route /new
gets executed and then the route with /:id
also getting invoked. I dont knw understand why its getting confused and triggers both routes. Please help. Thanks in advance.
This is because the route structure for both the endpoints are same. When you define a route /:id
, it accepts some X
value in the route. Now when you define your other as /new
, express will consider new
as the value of X
and call that route too. Workaround for this is to change the structure of one of route. You should follow rest pattern to avoid such problems. You can read here
Hope this helps :)