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" }
Example usage of the dot
Route path: /plantae/:genus.:species
Request URL: http://localhost:3000/plantae/Prunus.persica
req.params: { "genus": "Prunus", "species": "persica" }
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
In the code
app.get('/test', function(req,res){
console.log(req.query.array); // array=[a,b,c]
res.send(200);
});
Note that query string exposes sensitive data to clients, so sensitive data shouldn't be put into a query string.
Top comments (0)