如何在NodeJS中使用IPv6 https.request()

时间:2015-07-24 19:38:41

标签: javascript node.js https request ipv6

我编写了一个程序来运行我的团队正在开发的嵌入式设备的REST API。发出https请求的代码如下所示:

    var options = {
        hostname: hostAddress,
        port: hostPort,
        path: path,
        method: methodName,
        headers: {
            "Content-Type": "application/json",
            "Content-Length": post_data.length
        }
    };

    // Request and callback
    var request = https.request(
        options,
        function onResponse( response ) {
            // response code happens in here ...
        }
    );

hostAddress是IPv4地址时,此代码可以正常工作,例如“192.168.1.13”。但我们最近在嵌入式设备上添加了IPv6支持,并且还需要进行测试。但是,当hostAddress是IPv6地址时,上面的代码 not 不起作用。

https.request()[here:https://nodejs.org/api/https.html]的文档没有提及任何有关IPv6的内容。我已尝试对hostAddress使用以下每个值,但没有成功。

hostAddress = "fe80::9eb6:54ff:fe90:8b70%eth0";
hostAddress = "[fe80::9eb6:54ff:fe90:8b70%eth0]";
hostAddress = "fe80::9eb6:54ff:fe90:8b70";
hostAddress = "[fe80::9eb6:54ff:fe90:8b70]";

我知道IPv6地址是正确的 - 我从嵌入式系统主机上的ifconfig调用中复制并粘贴了它。

谁能告诉我这里缺少什么?

1 个答案:

答案 0 :(得分:3)

节点版本0.12在DNS绑定上不支持IPv6,并且由于request使用它,它也不起作用。

this thread,计划在v0.13中实施。

<强>更新

截至2015年,这似乎现在在主nodejs / node项目中实现(Node.js和io.js的重新合并)。

http = require('http');
server = http.createServer();
server.listen(9000, '::');