最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何在Express.js中传递URL中的字符串列表

IT培训 admin 6浏览 0评论

如何在Express.js中传递URL中的字符串列表

要在URL中传递单个参数,我在Postman中使用以下内容:

http://localhost:3000/api/prices/:shopId

这样可行!

现在,我想要做的是,用shopIds列表替换shopId。

对于我该如何实现这一点有任何想法?


伪代码:

URL for shopId = 1: http://localhost:3000/api/prices/1

URL for shopId = 2: http://localhost:3000/api/prices/2

我应该怎么做才能在单个API响应中获得shopId 1和2?

回答如下:

你最好的方法是传递数组的元素,这些元素用一个不会出现在任何单词中的字符(例如逗号)。

以此片段为例:

app.get('api/prices/:ids', function(req, res){
    var ids = req.params.ids.split(',');
    console.log(ids); //['shopId1', 'shopdId2']
})

您通过GET请求到达的端点:

http://localhost:3000/api/prices/shopId1,shopId2

如何在Express.js中传递URL中的字符串列表

要在URL中传递单个参数,我在Postman中使用以下内容:

http://localhost:3000/api/prices/:shopId

这样可行!

现在,我想要做的是,用shopIds列表替换shopId。

对于我该如何实现这一点有任何想法?


伪代码:

URL for shopId = 1: http://localhost:3000/api/prices/1

URL for shopId = 2: http://localhost:3000/api/prices/2

我应该怎么做才能在单个API响应中获得shopId 1和2?

回答如下:

你最好的方法是传递数组的元素,这些元素用一个不会出现在任何单词中的字符(例如逗号)。

以此片段为例:

app.get('api/prices/:ids', function(req, res){
    var ids = req.params.ids.split(',');
    console.log(ids); //['shopId1', 'shopdId2']
})

您通过GET请求到达的端点:

http://localhost:3000/api/prices/shopId1,shopId2

发布评论

评论列表 (0)

  1. 暂无评论