node.js http.request and ipv6 vs ipv4

后端 未结 1 1723
温柔的废话
温柔的废话 2021-01-07 02:31

In node.js 4.x for the function http.request the docs say

Options

  • family: IP address family to use when resolving hos
1条回答
  •  [愿得一人]
    2021-01-07 02:43

    TL;DR: it seems that Node leaves this up to the OS.

    Longer story: http.request() will use the net module under the hood. How that module handles DNS lookups can be found here: "if family isn't 4 and family isn't 6 and there are no special getaddrinfo hints provided, use dns.ADDRCONFIG as default lookup hint".

    dns.ADDRCONFIG means:

    Returned address types are determined by the types of addresses supported by the current system. For example, IPv4 addresses are only returned if the current system has at least one IPv4 address configured. Loopback addresses are not considered.

    This still doesn't say anything about the order in which the IP-addresses will be picked, but as far as I can tell, this is left to the implementation of getaddrinfo.

    AFAIK, if you have both IPv6 and IPv4 interfaces, two DNS queries will be performed to look up a hostname: A for IPv4 and AAAA for IPv6. It might be (but I'm guessing here) that the first lookup to succeed will supply the IP-address that is passed back to Node.

    Is there a way to get node.js to choose ipv6 when available or do I have to do with manually?

    It doesn't look like you can do that from http.request(), so yeah, I think you're going to have to do that manually by means of performing a DNS request with family : 6.

    0 讨论(0)
提交回复
热议问题