简体   繁体   中英

How to use IPv6 with NodeJS https.request()

I have written a program that exercises the REST API of an embedded device that my team is developing. The code that issues the https requests looks like this:

    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 ...
        }
    );

This code works just fine when hostAddress is an IPv4 address, eg "192.168.1.13". But we've recently added IPv6 support onto the embedded device and that needs to be tested as well. However, the same code above does not work when the hostAddress is an IPv6 address.

The documentation for https.request() [here: https://nodejs.org/api/https.html ] doesn't say anything about IPv6. I have tried using each of the following values for the hostAddress , but without success.

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]";

I know the IPv6 address is correct -- I copied and pasted it from an ifconfig call on the embedded system host.

Can anyone tell me what I'm missing here?

Node version 0.12 doesn't support IPv6 on DNS binding, and since request utilizes that, it also won't work.

Per this thread , it's scheduled for implementation in v0.13.

Update

As of 2015, this appears to now be implemented in the main nodejs/node project (the re-amalgamation of Node.js and io.js).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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