Alvin Alvin - 2 months ago 7
Node.js Question

why get is always hitting the first route

I am using express.

I have 2 routes:

router.get('/:id', function (req, res, next) {

router.get('/search', function (req, res, next) {


Every time I call GET
http://localhost:3000/users/search?name=@
I hit the fist route instead of the second one?

What should I change?

Answer

Because /:id matches anything include /search. It thinks the id is 'search'. Try putting the '/search' route first, or change it to /find/:id.

Comments