Express routes parameters

Basically, your declared routes are documented in the Express documentation.

The second route is resolved by a URL like /api/choice/hello where ‘hello’ is mapped into the req object object as:

router.get('/api/choice/:id', function (req, res) {
  console.log("choice id is " + req.params.id);
});

What you are actually trying is mapping query parameters.

A URL like /api/choice/?id=1 is resolved by the first router you provided.
Query parameters are easy to get mapped against the request as:

router.get('/api/choice', function (req, res) {
  console.log('id: ' + req.query.id);
  //get the whole query as!
  const queryStuff = JSON.stringify(req.query);
  console.log(queryStuff)
});

Leave a Comment