我刚刚在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>

看起来nodejs正在忽略localaddress并直接绑定到ipv4地址。
# node --version
v0.8.0

最佳答案

可以使用ipv6强制节点连接,至少在节点v0.12.7上有效。您的选项如下:

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

关于http - 如何将本地ipv6地址绑定(bind)到我的node.js程序?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11199776/

10-16 20:54