简体   繁体   English

如何访问Express.js中的可选URL参数?

[英]How to access optional URL parameters in Express.js?

In Express.js 4.14, I have the following route: 在Express.js 4.14中,我具有以下路线:

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: 如果我请求localhost:3000 / show / luis / arriojas / California / 123/456,我将收到:

{"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: 一切正常,但是如果我请求localhost:3000 / show / luis / California / 123,我将收到:

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

How could I get "California" as req.params.address instead of req.params.surname? 如何获得“加利福尼亚”作为req.params.address而不是req.params.surname?

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: 如果我请求localhost:3000/show/luis/123?surname=arriojas&address=California&phone=456 ,您将收到:

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

and if you request localhost:3000/show/luis/123&address=California , you will receive: 如果您请求localhost:3000/show/luis/123&address=California ,您将收到:

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

You have multiple consecutive optional parameters in your URL. 您的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. 当您按下localhost:3000 / show / luis / California / 123时,express无法知道您打算跳过哪些参数以及保留哪些参数。 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. 若要解决此问题,您可以更改程序设计,以接受所有参数作为查询字符串而不是url参数。 In which case you'd access your API with 'localhost:3000/show?name=luis&address=California&id=123', and your code you be like: 在这种情况下,您将使用“ localhost:3000 / show?name = luis&address = California&id = 123'访问您的API,并且您的代码如下所示:

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. 但是,如果要使用url参数,则必须在可选组件之间插入必填路径组件。 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'. 现在,您将以“ localhost:3000 / show / luis / at / California / 123”访问您的API。 Here, by knowing position of 'at' in url, express would correctly assign 'California' to address instead of surname. 在这里,通过知道url中“ at”的位置,express会正确地将“ California”分配给地址而不是姓。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM