我需要在节点服务器中进行http调用。可选参数为:
'名称='
这意味着url(相对路径)应如下所示:
/v1/clans?name=**exampleValue**
到目前为止,我的http请求的选项如下:
app.get('/v1/:clans?=:name', (req, res) => {
console.log(req.path)
const options = {
host: 'api.clashofclans.com',
path: req.path,
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer *token*'
}
};
const x = https.get(options, (request) => {...});
但这行不通。有人知道如何在我的path属性中包含可选参数吗?
最佳答案
你不知道那不是您要考虑的参数。这是一个查询参数,您的路径应如下所示:'/v1/clans
在您的情况下,使用req.query.<parameter>
检索查询参数req.query.name
您正在考虑的可选url参数类似于/v1/clans/:name
,并且可以使用req.params.name
访问。
关于node.js - Node.js-HTTP-带有可选参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50890682/