Express route parameters vs HTTP query parameters

Viewed 1825

I had always been wondering what kind of notation is the following,

GET /user/:name/books/:title

and how to interpret it until recently, when I learned that they are in the form of Express route and the ones with ":" are Express route parameters.

So here comes the question that I didn't find the answers from, say,

The question is, what if most or even all parameters are optional? How to handle that with Express route?

The problem is, with HTTP query parameters, like

https://example.org/?page=2&limit=3&sort=price

The order of query parameters can be arbitrary, whereas for Express route it seems to me the route parameters has to be specified in a very rigid way/order. So what if all route parameters are optional, and I just need to specify the last one? (no matter how you arrange the route parameters orders, there will always be a last one)

I did learn that Express can deal with querystring.parse(parsedUrl.query), but the reason I'm asking this question is really because of this -- https://github.com/gofiber/docs/blob/master/original/routing.md#parameters

I.e., gofiber follows/uses Express routing convention to handle route parameters, and I need all route parameters to be optional.

How to deal with that?

1 Answers

Every variable you send in URL with : is recieved in with req.params

This property is an object containing properties mapped to the named route “parameters”. For example, if you have the route /user/:name, then the “name” property is available as req.params.name. This object defaults to {}.

// will be available in route in req.params object
router.get('/somepath/with/:variable', (req ,res) => {
  console.log(req.params.variable);
});

Every variable you send in URL after ? (query params) will be available in req.query

This property is an object containing a property for each query string parameter in the route. When query parser is set to disabled, it is an empty object {}, otherwise it is the result of the configured query parser.

// will be available in route in req.query object
router.get('/somepath/with/variables?page=2&limit=3&sort=price', (req ,res) => {
  console.log(req.query.page);
  console.log(req.query.limit);
  console.log(req.query.sort);
});

every data you send through ajax or forms or something of the sort will be recieved with req.body

Contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as express.json() or express.urlencoded().

// will be available in route in req.body object
router.get('/somepath/with/variables', (req ,res) => {
  // send in request body like for example form data:
  console.log(req.body.variable1) // i.e
});

This are three objects that collect data in three different ways. You should just choose what's right for your sceneraio. I guess Route params isn't the right option and you should go with req.query when variables arrive randomly but you should know how to handle them once you have them parsed in the corresponding object.

I hope i understood the question and clarified.

Related