TIL: api route paths can be designed with the hypen (-) and the dot (.)

Daniel Lee - Sep 16 - - Dev Community

Today, I learned that API routes can be designed to use the hypen and dot for useful purposes. For instance, to pass/parse multiple pieces of information.

Example usage of the hypen

  Route path: /flights/:from-:to
  Request URL: http://localhost:3000/flights/LAX-SFO
  req.params: { "from": "LAX", "to": "SFO" }
Enter fullscreen mode Exit fullscreen mode

Example usage of the dot

  Route path: /plantae/:genus.:species
  Request URL: http://localhost:3000/plantae/Prunus.persica
  req.params: { "genus": "Prunus", "species": "persica" }
Enter fullscreen mode Exit fullscreen mode

There're other ways to pass multiple values to a route path such that uses an array or the same parameter.

Example

  Route path: /test
  Request URL1: https://localhost:3000/test?array=a&array=b&array=c
  Request URL2: https://localhost:3000/test?array[]=a&array[]=b&array[]=c
Enter fullscreen mode Exit fullscreen mode

In the code

  app.get('/test', function(req,res){
    console.log(req.query.array); // array=[a,b,c]
    res.send(200);
  });
Enter fullscreen mode Exit fullscreen mode

Note that query string exposes sensitive data to clients, so sensitive data shouldn't be put into a query string.

. . . . . . . . .
Terabox Video Player