Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Http 如何将本地ipv6地址绑定到node.js程序?_Http_Node.js_Request_Ipv6 - Fatal编程技术网

Http 如何将本地ipv6地址绑定到node.js程序?

Http 如何将本地ipv6地址绑定到node.js程序?,http,node.js,request,ipv6,Http,Node.js,Request,Ipv6,我刚刚在nodejs中创建了这个简单的程序,但是我无法将它绑定到我的NIC的ipv6地址 我在API文档中阅读了以下内容 localAddress:用于绑定网络连接的本地接口。 var http = require('http'); var options = { hostname: 'www.whatismyipv6.com', localAddress: '2a01:xxxx:xxxx:xxxx::2' //a real ipv6 address here }; var req

我刚刚在nodejs中创建了这个简单的程序,但是我无法将它绑定到我的NIC的ipv6地址

我在API文档中阅读了以下内容

localAddress:用于绑定网络连接的本地接口。

var http = require('http');

var options = {
  hostname: 'www.whatismyipv6.com',
  localAddress: '2a01:xxxx:xxxx:xxxx::2' //a real ipv6 address here
};

var req = http.request(options, function(res) {
  res.on('data', function (chunk) {
    console.log(chunk.toString());
  });
});

req.on('error', function(e) {
  console.log('ERROR: ' + e.message);
});

req.end();
但当我执行程序时,我得到了这个。请注意ipv4地址

<head>
<title>WhatIsMyIPv6? (IPv4: xx.xx.xxx.xxx)</title>
<meta name="bitly-verification" content="984886d337a6"/>
</head>
# node --version
v0.8.0

不幸的是,你在这一点上运气不好,而且任何其他主机都发布了A和AAAA RRs

http.js不会在堆栈中传递地址类型(地址族),因此在调用的底部,dns.js中的lookup()函数只发出一个常规的getaddrinfo()调用并获取第一个返回的结果,这是本例中目标主机IPv4地址的RR

如果选中dns.js,可以看到lookup()在未指定任何族的情况下,只会从结果中弹出地址[0]:

function onanswer(addresses) {
    if (addresses) {
      if (family) {
        callback(null, addresses[0], family);
      } else {
        callback(null, addresses[0], addresses[0].indexOf(':') >= 0 ? 6 : 4);
      }
    } else {
      callback(errnoException(process._errno, 'getaddrinfo'));
    }
  }
正如您所看到的,它确实努力为该结果设置族;在您的示例中,第一个结果被标识为IPv4系列,当它渗透备份时,堆栈绑定相应地完成,超过了您的localAddress规范


通过将目标主机更改为ipv6.whatismyipv6.com,您可以看到这一点;神奇的是,您的本地主机的IPv6地址现在可以根据需要绑定。

可以强制节点使用IPv6进行连接,至少它可以在节点v0.12.7上工作。您的选项如下所示:

var options = {
  hostname: 'www.whatismyipv6.com',
  localAddress: '2a01:xxxx:xxxx:xxxx::2', //a real ipv6 address here
  family: 6
};