简体   繁体   English

Node.js:HTTP获取请求

[英]Node.js: HTTP get request

I am trying to write a node.js script as follows 我正在尝试如下编写一个node.js脚本

var https = require('https');
var options = {
  hostname: 'https://172.16.2.51',
  port: 9090,
  path: '/vm/list',
  method: 'GET',                
};

var req = https.request(options, function(res) {
  res.on('data', function(d) {
    process.stdout.write(d);
  });
});

req.end();
req.on('error', function(e) {
  console.error(e);
});

When I run the script, I get this error: 运行脚本时,出现以下错误:

{ [Error: getaddrinfo ENOENT] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' }

The hostname property of options should be an IP address or a domain name. optionshostname属性应该是IP地址或域名。 In your example, get rid of the protocol. 在您的示例中,摆脱协议。 So change this: 所以改变这个:

var options = {
  hostname: 'https://172.16.2.51',
  port: 9090,
  path: '/vm/list',
  method: 'GET'
};

To this: 对此:

var options = {
  hostname: '172.16.2.51',
  port: 9090,
  path: '/vm/list',
  method: 'GET'
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM