IBM-使用带有Node.js的API创建VPC

我有一个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

mm00ll 回答:IBM-使用带有Node.js的API创建VPC

尝试如下:

const createExpoWebpackConfig = require("@expo/webpack-config");
module.exports = function(env,argv) {
  env.mode = "development";
  const config = createExpoWebpackConfig(env,argv);
  return config;
};

协议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(); 不应包含在主机字段中,建议您使用https。

本文链接:https://www.f2er.com/3157940.html

大家都在问