【问题标题】:IBM - Creating a VPC using API with Node.jsIBM - 使用 API 和 Node.js 创建 VPC
【发布时间】:2019-11-05 16:33:19
【问题描述】:

我有一个 Node.js 应用程序,目前允许用户配置 Digital Ocean Droplet。但是,我现在正尝试迁移到 IBM Cloud,而是想要供应虚拟服务器。

我遇到的问题是我没有使用 API 的经验。 Digital Ocean 有自己的 NPM 包,作为 Digital Ocean API 位的包装器,我找不到 IBM Cloud 的等价物。我一直在看VPC API documentation,我已经完成了使用终端创建虚拟服务器的整个过程,并且我已经成功配置了一个虚拟服务器。

现在,我正在尝试让这些 cURL 请求在 Node.js 中工作。我从简单的 GET images API 开始尝试打印可用的图像。该命令如下所示:

curl -X GET "https://eu-gb.iaas.cloud.ibm.com/v1/images?version=2019-10-08&generation=1" \
 -H "Authorization: *IAM TOKEN HERE*"

我已经阅读了Node HTTP documentation,到目前为止,我已将此命令转换为如下所示:

const http = require('http')

const options = {
  hostname: 'https://eu-gb.iaas.cloud.ibm.com',
  port: 80,
  path: '/v1/images?version=2019-10-08&generation=1',
  method: 'GET',
  headers: {
    'Authorization': '*IAM TOKEN HERE*'
  }
};

const req = http.request(options, (res) => {
  console.log(`STATUS: ${res.statusCode}`);
  console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
  res.on('end', () => {
    console.log('No more data in response.');
  });
});

req.on('error', (e) => {
  console.error(`problem with request: ${e.message}`);
});

req.end();

但是,当我运行 JS 文件时,出现以下错误:

problem with request: getaddrinfo ENOTFOUND https://eu-gb.iaas.cloud.ibm.com https://eu-gb.iaas.cloud.ibm.com:80

有人可以向我解释错误,我哪里出错了,我该如何解决这个问题?

非常感谢,

G

【问题讨论】:

    标签: node.js http ibm-cloud ibm-cloud-infrastructure


    【解决方案1】:

    尝试如下:

    const https = require('https');
    const options = {
        hostname: 'eu-gb.iaas.cloud.ibm.com',
        port:  443,
        path: '/v1/images?version=2019-10-08&generation=1',
        method: 'GET',
        headers: {
            'Authorization': 'Bearer <IAM TOKEN HERE>'
        }
    };
    const req = https.request(options, (res) => {
        console.log('statusCode:', res.statusCode);
        console.log('headers:', res.headers);
    
        res.on('data', (d) => {
            process.stdout.write(d);
        });
    });
    
    req.on('error', (e) => {
        console.error(e);
    });
    req.end();
    

    协议http://不应包含在主机字段中,建议使用https。

    【讨论】:

    • 谢谢!应该注意到一个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 2021-01-13
    • 2021-07-04
    • 2020-09-20
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多