Let's say I have get route like this:
app.get('/documents/format/type', function (req, res) { var format = req.params.format, type = req.params.type; });
So if I make request like
http://localhost:3000/documents/json/mini
in my format and type variables will be 'json' and 'mini' respectively, but if I make request like
http://localhost:3000/documents/mini/json
not. So my question is: how can I get the same variables in different order?
Your query parameters can be retrieved from the query object on the request object sent to your route. It is in the form of an object in which you can directly access the query parameters you care about. In this case Express handles all of the URL parsing for you and exposes the retrieved parameters as this object.
In Express, route parameters are essentially variables derived from named sections of the URL. Express captures the value in the named section and stores it in the req. params property. You can define multiple route parameters in a URL.
Your route isn't ok, it should be like this (with ':')
app.get('/documents/:format/:type', function (req, res) { var format = req.params.format, type = req.params.type; });
Also you cannot interchange parameter order unfortunately. For more information on req.params
(and req.query
) check out the api reference here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With