freeCodeCamp Challenge Guide: Get Route Parameter Input from the Client

Get Route Parameter Input from the Client


Problem Explanation

If someone tells you to build a GET or POST endpoint you would achieve the same using app.get(...) or app.post(...) accordingly.


Hints

Hint #1

In order to get route parameters from a POST request, the general format is as follows:

app.post("/:param1/:param2", (req, res) => {
  // Access the corresponding key in the req.params
  // object as defined in the endpoint
  var param1 = req.params.param1;
  // OR use destructuring to get multiple parameters
  var { param1, param2 } = req.params;
  // Send the req.params object as a JSON Response
  res.json(req.params);
});

Solutions

Solution 1 (Click to Show/Hide)
app.get("/:word/echo", (req, res) => {
  const { word } = req.params;
  res.json({
    echo: word
  });
});
5 Likes