简体   繁体   中英

Routes don't work in Express

I have two routes /emails and /eamils/:id :

var createRouter = function() {
  var router = express.Router();

  router.route('/emails/:id').get((req, res) => {
    console.log('get=>/emails/id');
  });

  router.route('/emails').get((req, res) => {
    console.log('get> /emails');
  });

  return router;
}

Whenever next request is sent the second handler gets called:

GET http://localhost:4000/rest-api/emails/?id=59

The first one which takes id parameter never works. How can I fix this?

The correct url should be:

http://localhost:4000/rest-api/emails/59

and not:

http://localhost:4000/rest-api/emails/?id=59

here id is query param.

You need to use:

http://localhost:4000/rest-api/emails/59

Query parameters don't count.

http://localhost:400/rest-api/emails/59 is correct. You are using params and not queries .

If you want to use queries their usage is like this after ? sign.

http://localhost:400/rest-api/emails?id=59

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM