代码之家  ›  专栏  ›  技术社区  ›  MarcoLe

nodejs-http-带可选参数

  •  0
  • MarcoLe  · 技术社区  · 6 年前

    我需要在节点服务器内进行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) => {...});
    

    但那不管用。有人知道如何在我的路径属性中包含可选参数吗?

    1 回复  |  直到 6 年前
        1
  •  4
  •   Adrian    6 年前

    你没有。那不是你想的参数。这是一个查询参数,您的路径应该如下所示:

    '/v1/clans

    您可以使用 req.query.<parameter> 以你为例 req.query.name

    您正在考虑的可选url参数如下 /v1/clans/:name 可以使用 req.params.name .