Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access optional URL parameters in Express.js?

In Express.js 4.14, I have the following route:

app.get('/show/:name/:surname?/:address?/:id/:phone?', function(req, res) {
    res.json({
        name: req.params.name,
        surname: req.params.surname,
        address: req.params.address,
        id: req.params.id,
        phone: req.params.phone
    });
});

If I request localhost:3000/show/luis/arriojas/California/123/456, I will receive:

{"name":"luis","surname":"arriojas","address":"California","id":"123","phone":"456"}

Everything is OK with that, but if I request localhost:3000/show/luis/California/123, I will receive:

{"name":"luis","surname":"California","id":"123"}

How could I get "California" as req.params.address instead of req.params.surname?

like image 646
Luis Arriojas Avatar asked Jul 06 '16 19:07

Luis Arriojas


People also ask

Can URL parameters be optional?

As query parameters are not a fixed part of a path, they can be optional and can have default values. In the example above they have default values of skip=0 and limit=10 . The parameter values in your function will be: skip=20 : because you set it in the URL.

How do I add an optional parameter to a URL?

Adding Optional Parameters to your Order Page URLs To add a third parameter, add another ampersand & , then the name of the parameter, and then the value for the parameter.

How do I accept URL parameters in node JS?

URL parameters can be in read in Node. js by importing the built-in url module, and using the URL and URLSearchParams objects present in it.

Can I pass URL as query parameter?

URL parameters (also called query string parameters or URL variables) are used to send small amounts of data from page to page, or from client to server via a URL. They can contain all kinds of useful information, such as search queries, link referrals, product information, user preferences, and more.


2 Answers

You have multiple consecutive optional parameters in your URL. When you hit localhost:3000/show/luis/California/123, express had no way of knowing which parameters you intended to skip, and which to keep. It ended up assigning parameters left to right, skipping assignment of the last unsatisfiable optional parameter.

To solve this issue, you can either change your program design to accept all the parameters as query string instead of url parameter. In which case you'd access your API with 'localhost:3000/show?name=luis&address=California&id=123', and your code you be like:

app.get('/show', function(req, res) {
    res.json({
        name: req.query.name,
        surname: req.query.surname,
        address: req.query.address,
        id: req.query.id,
        phone: req.query.phone
    });
});

However, if you want to use url parameters, you will have to insert a mandatory path component between optional components. Something like,

app.get('/show/:name/:surname?/at/:address?/:id/:phone?', function(req, res) {

Now you would access your API as 'localhost:3000/show/luis/at/California/123'. Here, by knowing position of 'at' in url, express would correctly assign 'California' to address instead of surname.

like image 111
d_shiv Avatar answered Sep 19 '22 20:09

d_shiv


app.get('/show/:name/:id/', function(req, res) {
    res.json({
        name: req.params.name,
        surname: req.query.surname,
        address: req.query.address,
        id: req.params.id,
        phone: req.query.phone
    });
});

If I you request localhost:3000/show/luis/123?surname=arriojas&address=California&phone=456, you will receive:

{"name":"luis","surname":"arriojas","address":"California","id":"123","phone":"456"}

and if you request localhost:3000/show/luis/123&address=California, you will receive:

{"name":"luis","surname":undefined, "address":"California","id":"123","phone":undefined}
like image 44
Emilio Platzer Avatar answered Sep 17 '22 20:09

Emilio Platzer