{ "source": "doc/api/dns.markdown", "modules": [ { "textRaw": "DNS", "name": "dns", "stability": 2, "stabilityText": "Stable", "desc": "
The dns
module contains functions belonging to two different categories:\n\n
1) Functions that use the underlying operating system facilities to perform\nname resolution, and that do not necessarily perform any network communication.\nThis category contains only one function: [dns.lookup()
][]. Developers\nlooking to perform name resolution in the same way that other applications on\nthe same operating system behave should use [dns.lookup()
][].\n\n
For example, looking up nodejs.org
.\n\n
const dns = require('dns');\n\ndns.lookup('nodejs.org', (err, addresses, family) => {\n console.log('addresses:', addresses);\n});
\n2) Functions that connect to an actual DNS server to perform name resolution,\nand that always use the network to perform DNS queries. This category\ncontains all functions in the dns
module except [dns.lookup()
][]. These\nfunctions do not use the same set of configuration files used by\n[dns.lookup()
][] (e.g. /etc/hosts
). These functions should be used by\ndevelopers who do not want to use the underlying operating system's facilities\nfor name resolution, and instead want to always perform DNS queries.\n\n
Below is an example that resolves 'nodejs.org'
then reverse resolves the IP\naddresses that are returned.\n\n
const dns = require('dns');\n\ndns.resolve4('nodejs.org', (err, addresses) => {\n if (err) throw err;\n\n console.log(`addresses: ${JSON.stringify(addresses)}`);\n\n addresses.forEach((a) => {\n dns.reverse(a, (err, hostnames) => {\n if (err) {\n throw err;\n }\n console.log(`reverse for ${a}: ${JSON.stringify(hostnames)}`);\n });\n });\n});
\nThere are subtle consequences in choosing one over the other, please consult\nthe [Implementation considerations section][] for more information.\n\n
\n", "methods": [ { "textRaw": "dns.getServers()", "type": "method", "name": "getServers", "desc": "Returns an array of IP address strings that are being used for name\nresolution.\n\n
\n", "signatures": [ { "params": [] } ] }, { "textRaw": "dns.lookup(hostname[, options], callback)", "type": "method", "name": "lookup", "desc": "Resolves a hostname (e.g. 'nodejs.org'
) into the first found A (IPv4) or\nAAAA (IPv6) record. options
can be an object or integer. If options
is\nnot provided, then IPv4 and IPv6 addresses are both valid. If options
is\nan integer, then it must be 4
or 6
.\n\n
Alternatively, options
can be an object containing these properties:\n\n
family
{Number} - The record family. If present, must be the integer\n4
or 6
. If not provided, both IP v4 and v6 addresses are accepted.hints
: {Number} - If present, it should be one or more of the supported\ngetaddrinfo
flags. If hints
is not provided, then no flags are passed to\ngetaddrinfo
. Multiple flags can be passed through hints
by logically\nOR
ing their values.\nSee [supported getaddrinfo
flags][] below for more information on supported\nflags.all
: {Boolean} - When true
, the callback returns all resolved addresses\nin an array, otherwise returns a single address. Defaults to false
.All properties are optional. An example usage of options is shown below.\n\n
\n{\n family: 4,\n hints: dns.ADDRCONFIG | dns.V4MAPPED,\n all: false\n}
\nThe callback
function has arguments (err, address, family)
. address
is a\nstring representation of an IPv4 or IPv6 address. family
is either the\ninteger 4
or 6
and denotes the family of address
(not necessarily the\nvalue initially passed to lookup
).\n\n
With the all
option set to true
, the arguments change to\n(err, addresses)
, with addresses
being an array of objects with the\nproperties address
and family
.\n\n
On error, err
is an [Error
][] object, where err.code
is the error code.\nKeep in mind that err.code
will be set to 'ENOENT'
not only when\nthe hostname does not exist but also when the lookup fails in other ways\nsuch as no available file descriptors.\n\n
dns.lookup()
does not necessarily have anything to do with the DNS protocol.\nThe implementation uses an operating system facility that can associate names\nwith addresses, and vice versa. This implementation can have subtle but\nimportant consequences on the behavior of any Node.js program. Please take some\ntime to consult the [Implementation considerations section][] before using\ndns.lookup()
.\n\n
The following flags can be passed as hints to [dns.lookup()
][].\n\n
dns.ADDRCONFIG
: Returned address types are determined by the types\nof addresses supported by the current system. For example, IPv4 addresses\nare only returned if the current system has at least one IPv4 address\nconfigured. Loopback addresses are not considered.dns.V4MAPPED
: If the IPv6 family was specified, but no IPv6 addresses were\nfound, then return IPv4 mapped IPv6 addresses. Note that it is not supported\non some operating systems (e.g FreeBSD 10.1).Resolves the given address
and port
into a hostname and service using\nthe operating system's underlying getnameinfo
implementation.\n\n
The callback has arguments (err, hostname, service)
. The hostname
and\nservice
arguments are strings (e.g. 'localhost'
and 'http'
respectively).\n\n
On error, err
is an [Error
][] object, where err.code
is the error code.\n\n
const dns = require('dns');\ndns.lookupService('127.0.0.1', 22, (err, hostname, service) => {\n console.log(hostname, service);\n // Prints: localhost ssh\n});
\n",
"signatures": [
{
"params": [
{
"name": "address"
},
{
"name": "port"
},
{
"name": "callback"
}
]
}
]
},
{
"textRaw": "dns.resolve(hostname[, rrtype], callback)",
"type": "method",
"name": "resolve",
"desc": "Uses the DNS protocol to resolve a hostname (e.g. 'nodejs.org'
) into an\narray of the record types specified by rrtype
.\n\n
Valid values for rrtype
are:\n\n
'A'
- IPV4 addresses, default'AAAA'
- IPV6 addresses'MX'
- mail exchange records'TXT'
- text records'SRV'
- SRV records'PTR'
- used for reverse IP lookups'NS'
- name server records'CNAME'
- canonical name records'SOA'
- start of authority recordThe callback
function has arguments (err, addresses)
. When successful,\naddresses
will be an array. The type of each item in addresses
is\ndetermined by the record type, and described in the documentation for the\ncorresponding lookup methods below.\n\n
On error, err
is an [Error
][] object, where err.code
is\none of the error codes listed below.\n\n
Uses the DNS protocol to resolve a IPv4 addresses (A
records) for the\nhostname
. The addresses
argument passed to the callback
function\nwill contain an array of IPv4 addresses (e.g.\n['74.125.79.104', '74.125.79.105', '74.125.79.106']
).\n\n
Uses the DNS protocol to resolve a IPv6 addresses (AAAA
records) for the\nhostname
. The addresses
argument passed to the callback
function\nwill contain an array of IPv6 addresses.\n\n
Uses the DNS protocol to resolve CNAME
records for the hostname
. The\naddresses
argument passed to the callback
function\nwill contain an of canonical name records available for the hostname
\n(e.g. ['bar.example.com']
).\n\n
Uses the DNS protocol to resolve mail exchange records (MX
records) for the\nhostname
. The addresses
argument passed to the callback
function will\ncontain an array of objects containing both a priority
and exchange
\nproperty (e.g. [{priority: 10, exchange: 'mx.example.com'}, ...]
).\n\n
Uses the DNS protocol to resolve name server records (NS
records) for the\nhostname
. The addresses
argument passed to the callback
function will\ncontain an array of name server records available for hostname
\n(e.g., ['ns1.example.com', 'ns2.example.com']
).\n\n
Uses the DNS protocol to resolve a start of authority record (SOA
record) for\nthe hostname
. The addresses
argument passed to the callback
function will\nbe an object with the following properties:\n\n
nsname
hostmaster
serial
refresh
retry
expire
minttl
{
\nnsname: 'ns.example.com',\nhostmaster: 'root.example.com',\nserial: 2013101809,\nrefresh: 10000,\nretry: 2400,\nexpire: 604800,\nminttl: 3600
\n}
\nUses the DNS protocol to resolve service records (SRV
records) for the\nhostname
. The addresses
argument passed to the callback
function will\nbe an array of objects with the following properties:\n\n
priority
weight
port
name
{
\npriority: 10,\nweight: 5,\nport: 21223,\nname: 'service.example.com'
\n}
\nUses the DNS protocol to resolve text queries (TXT
records) for the\nhostname
. The addresses
argument passed to the callback
function is\nis a two-dimentional array of the text records available for hostname
(e.g.,\n[ ['v=spf1 ip4:0.0.0.0 ', '~all' ] ]
). Each sub-array contains TXT chunks of\none record. Depending on the use case, these could be either joined together or\ntreated separately.\n\n
Performs a reverse DNS query that resolves an IPv4 or IPv6 address to an\narray of hostnames.\n\n
\nThe callback
function has arguments (err, hostnames)
, where hostnames
\nis an array of resolved hostnames for the given ip
.\n\n
On error, err
is an [Error
][] object, where err.code
is\none of the error codes listed below.\n\n
Sets the IP addresses of the servers to be used when resolving. The servers
\nargument is an array of IPv4 or IPv6 addresses.\n\n
If a port specified on the address it will be removed.\n\n
\nAn error will be thrown if an invalid address is provided.\n\n
\nThe dns.setServers()
method must not be called while a DNS query is in\nprogress.\n\n
Each DNS query can return one of the following error codes:\n\n
\ndns.NODATA
: DNS server returned answer with no data.dns.FORMERR
: DNS server claims query was misformatted.dns.SERVFAIL
: DNS server returned general failure.dns.NOTFOUND
: Domain name not found.dns.NOTIMP
: DNS server does not implement requested operation.dns.REFUSED
: DNS server refused query.dns.BADQUERY
: Misformatted DNS query.dns.BADNAME
: Misformatted hostname.dns.BADFAMILY
: Unsupported address family.dns.BADRESP
: Misformatted DNS reply.dns.CONNREFUSED
: Could not contact DNS servers.dns.TIMEOUT
: Timeout while contacting DNS servers.dns.EOF
: End of file.dns.FILE
: Error reading file.dns.NOMEM
: Out of memory.dns.DESTRUCTION
: Channel is being destroyed.dns.BADSTR
: Misformatted string.dns.BADFLAGS
: Illegal flags specified.dns.NONAME
: Given hostname is not numeric.dns.BADHINTS
: Illegal hints flags specified.dns.NOTINITIALIZED
: c-ares library initialization not yet performed.dns.LOADIPHLPAPI
: Error loading iphlpapi.dll.dns.ADDRGETNETWORKPARAMS
: Could not find GetNetworkParams function.dns.CANCELLED
: DNS query cancelled.Although [dns.lookup()
][] and the various dns.resolve*()/dns.reverse()
\nfunctions have the same goal of associating a network name with a network\naddress (or vice versa), their behavior is quite different. These differences\ncan have subtle but significant consequences on the behavior of Node.js\nprograms.\n\n
Under the hood, [dns.lookup()
][] uses the same operating system facilities\nas most other programs. For instance, [dns.lookup()
][] will almost always\nresolve a given name the same way as the ping
command. On most POSIX-like\noperating systems, the behavior of the [dns.lookup()
][] function can be\nmodified by changing settings in nsswitch.conf(5)
and/or resolv.conf(5)
,\nbut note that changing these files will change the behavior of all other\nprograms running on the same operating system.\n\n
Though the call to dns.lookup()
will be asynchronous from JavaScript's\nperspective, it is implemented as a synchronous call to getaddrinfo(3)
that\nruns on libuv's threadpool. Because libuv's threadpool has a fixed size, it\nmeans that if for whatever reason the call to getaddrinfo(3)
takes a long\ntime, other operations that could run on libuv's threadpool (such as filesystem\noperations) will experience degraded performance. In order to mitigate this\nissue, one potential solution is to increase the size of libuv's threadpool by\nsetting the 'UV_THREADPOOL_SIZE'
environment variable to a value greater than\n4
(its current default value). For more information on libuv's threadpool, see\n[the official libuv documentation][].\n\n
These functions are implemented quite differently than [dns.lookup()
][]. They\ndo not use getaddrinfo(3)
and they always perform a DNS query on the\nnetwork. This network communication is always done asynchronously, and does not\nuse libuv's threadpool.\n\n
As a result, these functions cannot have the same negative impact on other\nprocessing that happens on libuv's threadpool that [dns.lookup()
][] can have.\n\n
They do not use the same set of configuration files than what [dns.lookup()
][]\nuses. For instance, they do not use the configuration from /etc/hosts
.\n\n