{ "type": "module", "source": "doc/api/dns.md", "modules": [ { "textRaw": "DNS", "name": "dns", "introduced_in": "v0.10.0", "stability": 2, "stabilityText": "Stable", "desc": "
Source Code: lib/dns.js
\nThe dns
module enables name resolution. For example, use it to look up IP\naddresses of host names.
Although named for the Domain Name System (DNS), it does not always use the\nDNS protocol for lookups. dns.lookup()
uses the operating system\nfacilities to perform name resolution. It may not need to perform any network\ncommunication. To perform name resolution the way other applications on the same\nsystem do, use dns.lookup()
.
const dns = require('dns');\n\ndns.lookup('example.org', (err, address, family) => {\n console.log('address: %j family: IPv%s', address, family);\n});\n// address: \"93.184.216.34\" family: IPv4\n
\nAll other functions in the dns
module connect to an actual DNS server to\nperform name resolution. They will always use the network to perform DNS\nqueries. These functions do not use the same set of configuration files used by\ndns.lookup()
(e.g. /etc/hosts
). Use these functions to always perform\nDNS queries, bypassing other name-resolution facilities.
const dns = require('dns');\n\ndns.resolve4('archive.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});\n
\nSee the Implementation considerations section for more information.
", "classes": [ { "textRaw": "Class: `dns.Resolver`", "type": "class", "name": "dns.Resolver", "meta": { "added": [ "v8.3.0" ], "changes": [] }, "desc": "An independent resolver for DNS requests.
\nCreating a new resolver uses the default server settings. Setting\nthe servers used for a resolver using\nresolver.setServers()
does not affect\nother resolvers:
const { Resolver } = require('dns');\nconst resolver = new Resolver();\nresolver.setServers(['4.4.4.4']);\n\n// This request will use the server at 4.4.4.4, independent of global settings.\nresolver.resolve4('example.org', (err, addresses) => {\n // ...\n});\n
\nThe following methods from the dns
module are available:
resolver.getServers()
resolver.resolve()
resolver.resolve4()
resolver.resolve6()
resolver.resolveAny()
resolver.resolveCaa()
resolver.resolveCname()
resolver.resolveMx()
resolver.resolveNaptr()
resolver.resolveNs()
resolver.resolvePtr()
resolver.resolveSoa()
resolver.resolveSrv()
resolver.resolveTxt()
resolver.reverse()
resolver.setServers()
Create a new resolver.
\noptions
<Object>\n\nCancel all outstanding DNS queries made by this resolver. The corresponding\ncallbacks will be called with an error with code ECANCELLED
.
The resolver instance will send its requests from the specified IP address.\nThis allows programs to specify outbound interfaces when used on multi-homed\nsystems.
\nIf a v4 or v6 address is not specified, it is set to the default, and the\noperating system will choose a local address automatically.
\nThe resolver will use the v4 local address when making requests to IPv4 DNS\nservers, and the v6 local address when making requests to IPv6 DNS servers.\nThe rrtype
of resolution requests has no impact on the local address used.
Returns an array of IP address strings, formatted according to RFC 5952,\nthat are currently configured for DNS resolution. A string will include a port\nsection if a custom port is used.
\n\n[\n '4.4.4.4',\n '2001:4860:4860::8888',\n '4.4.4.4:1053',\n '[2001:4860:4860::8888]:1053',\n]\n
"
},
{
"textRaw": "`dns.lookup(hostname[, options], callback)`",
"type": "method",
"name": "lookup",
"meta": {
"added": [
"v0.1.90"
],
"changes": [
{
"version": "v8.5.0",
"pr-url": "https://github.com/nodejs/node/pull/14731",
"description": "The `verbatim` option is supported now."
},
{
"version": "v1.2.0",
"pr-url": "https://github.com/nodejs/node/pull/744",
"description": "The `all` option is supported now."
}
]
},
"signatures": [
{
"params": [
{
"textRaw": "`hostname` {string}",
"name": "hostname",
"type": "string"
},
{
"textRaw": "`options` {integer | Object}",
"name": "options",
"type": "integer | Object",
"options": [
{
"textRaw": "`family` {integer} The record family. Must be `4`, `6`, or `0`. The value `0` indicates that IPv4 and IPv6 addresses are both returned. **Default:** `0`.",
"name": "family",
"type": "integer",
"default": "`0`",
"desc": "The record family. Must be `4`, `6`, or `0`. The value `0` indicates that IPv4 and IPv6 addresses are both returned."
},
{
"textRaw": "`hints` {number} One or more [supported `getaddrinfo` flags][]. Multiple flags may be passed by bitwise `OR`ing their values.",
"name": "hints",
"type": "number",
"desc": "One or more [supported `getaddrinfo` flags][]. Multiple flags may be passed by bitwise `OR`ing their values."
},
{
"textRaw": "`all` {boolean} When `true`, the callback returns all resolved addresses in an array. Otherwise, returns a single address. **Default:** `false`.",
"name": "all",
"type": "boolean",
"default": "`false`",
"desc": "When `true`, the callback returns all resolved addresses in an array. Otherwise, returns a single address."
},
{
"textRaw": "`verbatim` {boolean} When `true`, the callback receives IPv4 and IPv6 addresses in the order the DNS resolver returned them. When `false`, IPv4 addresses are placed before IPv6 addresses. **Default:** currently `false` (addresses are reordered) but this is expected to change in the not too distant future. Default value is configurable using [`dns.setDefaultResultOrder()`][] or [`--dns-result-order`][]. New code should use `{ verbatim: true }`.",
"name": "verbatim",
"type": "boolean",
"default": "currently `false` (addresses are reordered) but this is expected to change in the not too distant future. Default value is configurable using [`dns.setDefaultResultOrder()`][] or [`--dns-result-order`][]. New code should use `{ verbatim: true }`",
"desc": "When `true`, the callback receives IPv4 and IPv6 addresses in the order the DNS resolver returned them. When `false`, IPv4 addresses are placed before IPv6 addresses."
}
]
},
{
"textRaw": "`callback` {Function}",
"name": "callback",
"type": "Function",
"options": [
{
"textRaw": "`err` {Error}",
"name": "err",
"type": "Error"
},
{
"textRaw": "`address` {string} A string representation of an IPv4 or IPv6 address.",
"name": "address",
"type": "string",
"desc": "A string representation of an IPv4 or IPv6 address."
},
{
"textRaw": "`family` {integer} `4` or `6`, denoting the family of `address`, or `0` if the address is not an IPv4 or IPv6 address. `0` is a likely indicator of a bug in the name resolution service used by the operating system.",
"name": "family",
"type": "integer",
"desc": "`4` or `6`, denoting the family of `address`, or `0` if the address is not an IPv4 or IPv6 address. `0` is a likely indicator of a bug in the name resolution service used by the operating system."
}
]
}
]
}
],
"desc": "Resolves a host name (e.g. 'nodejs.org'
) into the first found A (IPv4) or\nAAAA (IPv6) record. All option
properties are optional. If options
is an\ninteger, then it must be 4
or 6
– if options
is not provided, then IPv4\nand IPv6 addresses are both returned if found.
With the all
option set to true
, the arguments for callback
change to\n(err, addresses)
, with addresses
being an array of objects with the\nproperties address
and family
.
On error, err
is an Error
object, where err.code
is the error code.\nKeep in mind that err.code
will be set to 'ENOTFOUND'
not only when\nthe host name does not exist but also when the lookup fails in other ways\nsuch as no available file descriptors.
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()
.
Example usage:
\nconst dns = require('dns');\nconst options = {\n family: 6,\n hints: dns.ADDRCONFIG | dns.V4MAPPED,\n};\ndns.lookup('example.com', options, (err, address, family) =>\n console.log('address: %j family: IPv%s', address, family));\n// address: \"2606:2800:220:1:248:1893:25c8:1946\" family: IPv6\n\n// When options.all is true, the result will be an Array.\noptions.all = true;\ndns.lookup('example.com', options, (err, addresses) =>\n console.log('addresses: %j', addresses));\n// addresses: [{\"address\":\"2606:2800:220:1:248:1893:25c8:1946\",\"family\":6}]\n
\nIf this method is invoked as its util.promisify()
ed version, and all
\nis not set to true
, it returns a Promise
for an Object
with address
and\nfamily
properties.
The following flags can be passed as hints to dns.lookup()
.
dns.ADDRCONFIG
: Limits returned address types to the types of non-loopback\naddresses configured on the system. For example, IPv4 addresses are only\nreturned if the current system has at least one IPv4 address configured.dns.V4MAPPED
: If the IPv6 family was specified, but no IPv6 addresses were\nfound, then return IPv4 mapped IPv6 addresses. It is not supported\non some operating systems (e.g FreeBSD 10.1).dns.ALL
: If dns.V4MAPPED
is specified, return resolved IPv6 addresses as\nwell as IPv4 mapped IPv6 addresses.Resolves the given address
and port
into a host name and service using\nthe operating system's underlying getnameinfo
implementation.
If address
is not a valid IP address, a TypeError
will be thrown.\nThe port
will be coerced to a number. If it is not a legal port, a TypeError
\nwill be thrown.
On an error, err
is an Error
object, where err.code
is the error code.
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
\nIf this method is invoked as its util.promisify()
ed version, it returns a\nPromise
for an Object
with hostname
and service
properties.
Uses the DNS protocol to resolve a host name (e.g. 'nodejs.org'
) into an array\nof the resource records. The callback
function has arguments\n(err, records)
. When successful, records
will be an array of resource\nrecords. The type and structure of individual results varies based on rrtype
:
rrtype | \nrecords contains | \nResult type | \nShorthand method | \n
---|---|---|---|
'A' | \nIPv4 addresses (default) | \n<string> | \ndns.resolve4() | \n
'AAAA' | \nIPv6 addresses | \n<string> | \ndns.resolve6() | \n
'ANY' | \nany records | \n<Object> | \ndns.resolveAny() | \n
'CAA' | \nCA authorization records | \n<Object> | \ndns.resolveCaa() | \n
'CNAME' | \ncanonical name records | \n<string> | \ndns.resolveCname() | \n
'MX' | \nmail exchange records | \n<Object> | \ndns.resolveMx() | \n
'NAPTR' | \nname authority pointer records | \n<Object> | \ndns.resolveNaptr() | \n
'NS' | \nname server records | \n<string> | \ndns.resolveNs() | \n
'PTR' | \npointer records | \n<string> | \ndns.resolvePtr() | \n
'SOA' | \nstart of authority records | \n<Object> | \ndns.resolveSoa() | \n
'SRV' | \nservice records | \n<Object> | \ndns.resolveSrv() | \n
'TXT' | \ntext records | \n<string[]> | \ndns.resolveTxt() | \n
On error, err
is an Error
object, where err.code
is one of the\nDNS error codes.
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']
).
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.
Uses the DNS protocol to resolve all records (also known as ANY
or *
query).\nThe ret
argument passed to the callback
function will be an array containing\nvarious types of records. Each object has a property type
that indicates the\ntype of the current record. And depending on the type
, additional properties\nwill be present on the object:
Type | \nProperties | \n
---|---|
'A' | \naddress /ttl | \n
'AAAA' | \naddress /ttl | \n
'CNAME' | \nvalue | \n
'MX' | \nRefer to dns.resolveMx() | \n
'NAPTR' | \nRefer to dns.resolveNaptr() | \n
'NS' | \nvalue | \n
'PTR' | \nvalue | \n
'SOA' | \nRefer to dns.resolveSoa() | \n
'SRV' | \nRefer to dns.resolveSrv() | \n
'TXT' | \nThis type of record contains an array property called entries which refers to dns.resolveTxt() , e.g. { entries: ['...'], type: 'TXT' } | \n
Here is an example of the ret
object passed to the callback:
[ { type: 'A', address: '127.0.0.1', ttl: 299 },\n { type: 'CNAME', value: 'example.com' },\n { type: 'MX', exchange: 'alt4.aspmx.l.example.com', priority: 50 },\n { type: 'NS', value: 'ns1.example.com' },\n { type: 'TXT', entries: [ 'v=spf1 include:_spf.example.com ~all' ] },\n { type: 'SOA',\n nsname: 'ns1.example.com',\n hostmaster: 'admin.example.com',\n serial: 156696742,\n refresh: 900,\n retry: 900,\n expire: 1800,\n minttl: 60 } ]\n
\nDNS server operators may choose not to respond to ANY
\nqueries. It may be better to call individual methods like dns.resolve4()
,\ndns.resolveMx()
, and so on. For more details, see RFC 8482.
Uses the DNS protocol to resolve CNAME
records for the hostname
. The\naddresses
argument passed to the callback
function\nwill contain an array of canonical name records available for the hostname
\n(e.g. ['bar.example.com']
).
Uses the DNS protocol to resolve CAA
records for the hostname
. The\naddresses
argument passed to the callback
function\nwill contain an array of certification authority authorization records\navailable for the hostname
(e.g. [{critical: 0, iodef: 'mailto:pki@example.com'}, {critical: 128, issue: 'pki.example.com'}]
).
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'}, ...]
).
Uses the DNS protocol to resolve regular expression based records (NAPTR
\nrecords) for the hostname
. The addresses
argument passed to the callback
\nfunction will contain an array of objects with the following properties:
flags
service
regexp
replacement
order
preference
{\n flags: 's',\n service: 'SIP+D2U',\n regexp: '',\n replacement: '_sip._udp.example.com',\n order: 30,\n preference: 100\n}\n
"
},
{
"textRaw": "`dns.resolveNs(hostname, callback)`",
"type": "method",
"name": "resolveNs",
"meta": {
"added": [
"v0.1.90"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`hostname` {string}",
"name": "hostname",
"type": "string"
},
{
"textRaw": "`callback` {Function}",
"name": "callback",
"type": "Function",
"options": [
{
"textRaw": "`err` {Error}",
"name": "err",
"type": "Error"
},
{
"textRaw": "`addresses` {string[]}",
"name": "addresses",
"type": "string[]"
}
]
}
]
}
],
"desc": "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']
).
Uses the DNS protocol to resolve pointer records (PTR
records) for the\nhostname
. The addresses
argument passed to the callback
function will\nbe an array of strings containing the reply records.
Uses the DNS protocol to resolve a start of authority record (SOA
record) for\nthe hostname
. The address
argument passed to the callback
function will\nbe an object with the following properties:
nsname
hostmaster
serial
refresh
retry
expire
minttl
{\n nsname: 'ns.example.com',\n hostmaster: 'root.example.com',\n serial: 2013101809,\n refresh: 10000,\n retry: 2400,\n expire: 604800,\n minttl: 3600\n}\n
"
},
{
"textRaw": "`dns.resolveSrv(hostname, callback)`",
"type": "method",
"name": "resolveSrv",
"meta": {
"added": [
"v0.1.27"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`hostname` {string}",
"name": "hostname",
"type": "string"
},
{
"textRaw": "`callback` {Function}",
"name": "callback",
"type": "Function",
"options": [
{
"textRaw": "`err` {Error}",
"name": "err",
"type": "Error"
},
{
"textRaw": "`addresses` {Object[]}",
"name": "addresses",
"type": "Object[]"
}
]
}
]
}
],
"desc": "Uses 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:
priority
weight
port
name
{\n priority: 10,\n weight: 5,\n port: 21223,\n name: 'service.example.com'\n}\n
"
},
{
"textRaw": "`dns.resolveTxt(hostname, callback)`",
"type": "method",
"name": "resolveTxt",
"meta": {
"added": [
"v0.1.27"
],
"changes": []
},
"signatures": [
{
"params": []
}
],
"desc": "\nhostname
<string>callback
<Function>\nerr
<Error>records
<string[][]>Uses the DNS protocol to resolve text queries (TXT
records) for the\nhostname
. The records
argument passed to the callback
function is a\ntwo-dimensional 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.
Performs a reverse DNS query that resolves an IPv4 or IPv6 address to an\narray of host names.
\nOn error, err
is an Error
object, where err.code
is\none of the DNS error codes.
Set the default value of verbatim
in dns.lookup()
and\ndnsPromises.lookup()
. The value could be:
ipv4first
: sets default verbatim
false
.verbatim
: sets default verbatim
true
.The default is ipv4first
and dns.setDefaultResultOrder()
have higher\npriority than --dns-result-order
. When using worker threads,\ndns.setDefaultResultOrder()
from the main thread won't affect the default\ndns orders in workers.
Sets the IP address and port of servers to be used when performing DNS\nresolution. The servers
argument is an array of RFC 5952 formatted\naddresses. If the port is the IANA default DNS port (53) it can be omitted.
dns.setServers([\n '4.4.4.4',\n '[2001:4860:4860::8888]',\n '4.4.4.4:1053',\n '[2001:4860:4860::8888]:1053',\n]);\n
\nAn error will be thrown if an invalid address is provided.
\nThe dns.setServers()
method must not be called while a DNS query is in\nprogress.
The dns.setServers()
method affects only dns.resolve()
,\ndns.resolve*()
and dns.reverse()
(and specifically not\ndns.lookup()
).
This method works much like\nresolve.conf.\nThat is, if attempting to resolve with the first server provided results in a\nNOTFOUND
error, the resolve()
method will not attempt to resolve with\nsubsequent servers provided. Fallback DNS servers will only be used if the\nearlier ones time out or result in some other error.
The dns.promises
API provides an alternative set of asynchronous DNS methods\nthat return Promise
objects rather than using callbacks. The API is accessible\nvia require('dns').promises
.
An independent resolver for DNS requests.
\nCreating a new resolver uses the default server settings. Setting\nthe servers used for a resolver using\nresolver.setServers()
does not affect\nother resolvers:
const { Resolver } = require('dns').promises;\nconst resolver = new Resolver();\nresolver.setServers(['4.4.4.4']);\n\n// This request will use the server at 4.4.4.4, independent of global settings.\nresolver.resolve4('example.org').then((addresses) => {\n // ...\n});\n\n// Alternatively, the same code can be written using async-await style.\n(async function() {\n const addresses = await resolver.resolve4('example.org');\n})();\n
\nThe following methods from the dnsPromises
API are available:
resolver.getServers()
resolver.resolve()
resolver.resolve4()
resolver.resolve6()
resolver.resolveAny()
resolver.resolveCaa()
resolver.resolveCname()
resolver.resolveMx()
resolver.resolveNaptr()
resolver.resolveNs()
resolver.resolvePtr()
resolver.resolveSoa()
resolver.resolveSrv()
resolver.resolveTxt()
resolver.reverse()
resolver.setServers()
Cancel all outstanding DNS queries made by this resolver. The corresponding\npromises will be rejected with an error with code ECANCELLED
.
Returns an array of IP address strings, formatted according to RFC 5952,\nthat are currently configured for DNS resolution. A string will include a port\nsection if a custom port is used.
\n\n[\n '4.4.4.4',\n '2001:4860:4860::8888',\n '4.4.4.4:1053',\n '[2001:4860:4860::8888]:1053',\n]\n
"
},
{
"textRaw": "`dnsPromises.lookup(hostname[, options])`",
"type": "method",
"name": "lookup",
"meta": {
"added": [
"v10.6.0"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`hostname` {string}",
"name": "hostname",
"type": "string"
},
{
"textRaw": "`options` {integer | Object}",
"name": "options",
"type": "integer | Object",
"options": [
{
"textRaw": "`family` {integer} The record family. Must be `4`, `6`, or `0`. The value `0` indicates that IPv4 and IPv6 addresses are both returned. **Default:** `0`.",
"name": "family",
"type": "integer",
"default": "`0`",
"desc": "The record family. Must be `4`, `6`, or `0`. The value `0` indicates that IPv4 and IPv6 addresses are both returned."
},
{
"textRaw": "`hints` {number} One or more [supported `getaddrinfo` flags][]. Multiple flags may be passed by bitwise `OR`ing their values.",
"name": "hints",
"type": "number",
"desc": "One or more [supported `getaddrinfo` flags][]. Multiple flags may be passed by bitwise `OR`ing their values."
},
{
"textRaw": "`all` {boolean} When `true`, the `Promise` is resolved with all addresses in an array. Otherwise, returns a single address. **Default:** `false`.",
"name": "all",
"type": "boolean",
"default": "`false`",
"desc": "When `true`, the `Promise` is resolved with all addresses in an array. Otherwise, returns a single address."
},
{
"textRaw": "`verbatim` {boolean} When `true`, the `Promise` is resolved with IPv4 and IPv6 addresses in the order the DNS resolver returned them. When `false`, IPv4 addresses are placed before IPv6 addresses. **Default:** currently `false` (addresses are reordered) but this is expected to change in the not too distant future. Default value is configurable using [`dns.setDefaultResultOrder()`][] or [`--dns-result-order`][]. New code should use `{ verbatim: true }`.",
"name": "verbatim",
"type": "boolean",
"default": "currently `false` (addresses are reordered) but this is expected to change in the not too distant future. Default value is configurable using [`dns.setDefaultResultOrder()`][] or [`--dns-result-order`][]. New code should use `{ verbatim: true }`",
"desc": "When `true`, the `Promise` is resolved with IPv4 and IPv6 addresses in the order the DNS resolver returned them. When `false`, IPv4 addresses are placed before IPv6 addresses."
}
]
}
]
}
],
"desc": "Resolves a host name (e.g. 'nodejs.org'
) into the first found A (IPv4) or\nAAAA (IPv6) record. All option
properties are optional. If options
is an\ninteger, then it must be 4
or 6
– if options
is not provided, then IPv4\nand IPv6 addresses are both returned if found.
With the all
option set to true
, the Promise
is resolved with addresses
\nbeing an array of objects with the properties address
and family
.
On error, the Promise
is rejected with an Error
object, where err.code
\nis the error code.\nKeep in mind that err.code
will be set to 'ENOTFOUND'
not only when\nthe host name does not exist but also when the lookup fails in other ways\nsuch as no available file descriptors.
dnsPromises.lookup()
does not necessarily have anything to do with the DNS\nprotocol. The implementation uses an operating system facility that can\nassociate names with addresses, and vice versa. This implementation can have\nsubtle but important consequences on the behavior of any Node.js program. Please\ntake some time to consult the Implementation considerations section before\nusing dnsPromises.lookup()
.
Example usage:
\nconst dns = require('dns');\nconst dnsPromises = dns.promises;\nconst options = {\n family: 6,\n hints: dns.ADDRCONFIG | dns.V4MAPPED,\n};\n\ndnsPromises.lookup('example.com', options).then((result) => {\n console.log('address: %j family: IPv%s', result.address, result.family);\n // address: \"2606:2800:220:1:248:1893:25c8:1946\" family: IPv6\n});\n\n// When options.all is true, the result will be an Array.\noptions.all = true;\ndnsPromises.lookup('example.com', options).then((result) => {\n console.log('addresses: %j', result);\n // addresses: [{\"address\":\"2606:2800:220:1:248:1893:25c8:1946\",\"family\":6}]\n});\n
"
},
{
"textRaw": "`dnsPromises.lookupService(address, port)`",
"type": "method",
"name": "lookupService",
"meta": {
"added": [
"v10.6.0"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`address` {string}",
"name": "address",
"type": "string"
},
{
"textRaw": "`port` {number}",
"name": "port",
"type": "number"
}
]
}
],
"desc": "Resolves the given address
and port
into a host name and service using\nthe operating system's underlying getnameinfo
implementation.
If address
is not a valid IP address, a TypeError
will be thrown.\nThe port
will be coerced to a number. If it is not a legal port, a TypeError
\nwill be thrown.
On error, the Promise
is rejected with an Error
object, where err.code
\nis the error code.
const dnsPromises = require('dns').promises;\ndnsPromises.lookupService('127.0.0.1', 22).then((result) => {\n console.log(result.hostname, result.service);\n // Prints: localhost ssh\n});\n
"
},
{
"textRaw": "`dnsPromises.resolve(hostname[, rrtype])`",
"type": "method",
"name": "resolve",
"meta": {
"added": [
"v10.6.0"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`hostname` {string} Host name to resolve.",
"name": "hostname",
"type": "string",
"desc": "Host name to resolve."
},
{
"textRaw": "`rrtype` {string} Resource record type. **Default:** `'A'`.",
"name": "rrtype",
"type": "string",
"default": "`'A'`",
"desc": "Resource record type."
}
]
}
],
"desc": "Uses the DNS protocol to resolve a host name (e.g. 'nodejs.org'
) into an array\nof the resource records. When successful, the Promise
is resolved with an\narray of resource records. The type and structure of individual results vary\nbased on rrtype
:
rrtype | \nrecords contains | \nResult type | \nShorthand method | \n
---|---|---|---|
'A' | \nIPv4 addresses (default) | \n<string> | \ndnsPromises.resolve4() | \n
'AAAA' | \nIPv6 addresses | \n<string> | \ndnsPromises.resolve6() | \n
'ANY' | \nany records | \n<Object> | \ndnsPromises.resolveAny() | \n
'CAA' | \nCA authorization records | \n<Object> | \ndnsPromises.resolveCaa() | \n
'CNAME' | \ncanonical name records | \n<string> | \ndnsPromises.resolveCname() | \n
'MX' | \nmail exchange records | \n<Object> | \ndnsPromises.resolveMx() | \n
'NAPTR' | \nname authority pointer records | \n<Object> | \ndnsPromises.resolveNaptr() | \n
'NS' | \nname server records | \n<string> | \ndnsPromises.resolveNs() | \n
'PTR' | \npointer records | \n<string> | \ndnsPromises.resolvePtr() | \n
'SOA' | \nstart of authority records | \n<Object> | \ndnsPromises.resolveSoa() | \n
'SRV' | \nservice records | \n<Object> | \ndnsPromises.resolveSrv() | \n
'TXT' | \ntext records | \n<string[]> | \ndnsPromises.resolveTxt() | \n
On error, the Promise
is rejected with an Error
object, where err.code
\nis one of the DNS error codes.
Uses the DNS protocol to resolve IPv4 addresses (A
records) for the\nhostname
. On success, the Promise
is resolved with an array of IPv4\naddresses (e.g. ['74.125.79.104', '74.125.79.105', '74.125.79.106']
).
Uses the DNS protocol to resolve IPv6 addresses (AAAA
records) for the\nhostname
. On success, the Promise
is resolved with an array of IPv6\naddresses.
Uses the DNS protocol to resolve all records (also known as ANY
or *
query).\nOn success, the Promise
is resolved with an array containing various types of\nrecords. Each object has a property type
that indicates the type of the\ncurrent record. And depending on the type
, additional properties will be\npresent on the object:
Type | \nProperties | \n
---|---|
'A' | \naddress /ttl | \n
'AAAA' | \naddress /ttl | \n
'CNAME' | \nvalue | \n
'MX' | \nRefer to dnsPromises.resolveMx() | \n
'NAPTR' | \nRefer to dnsPromises.resolveNaptr() | \n
'NS' | \nvalue | \n
'PTR' | \nvalue | \n
'SOA' | \nRefer to dnsPromises.resolveSoa() | \n
'SRV' | \nRefer to dnsPromises.resolveSrv() | \n
'TXT' | \nThis type of record contains an array property called entries which refers to dnsPromises.resolveTxt() , e.g. { entries: ['...'], type: 'TXT' } | \n
Here is an example of the result object:
\n\n[ { type: 'A', address: '127.0.0.1', ttl: 299 },\n { type: 'CNAME', value: 'example.com' },\n { type: 'MX', exchange: 'alt4.aspmx.l.example.com', priority: 50 },\n { type: 'NS', value: 'ns1.example.com' },\n { type: 'TXT', entries: [ 'v=spf1 include:_spf.example.com ~all' ] },\n { type: 'SOA',\n nsname: 'ns1.example.com',\n hostmaster: 'admin.example.com',\n serial: 156696742,\n refresh: 900,\n retry: 900,\n expire: 1800,\n minttl: 60 } ]\n
"
},
{
"textRaw": "`dnsPromises.resolveCaa(hostname)`",
"type": "method",
"name": "resolveCaa",
"meta": {
"added": [
"v14.17.0"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`hostname` {string}",
"name": "hostname",
"type": "string"
}
]
}
],
"desc": "Uses the DNS protocol to resolve CAA
records for the hostname
. On success,\nthe Promise
is resolved with an array of objects containing available\ncertification authority authorization records available for the hostname
\n(e.g. [{critical: 0, iodef: 'mailto:pki@example.com'},{critical: 128, issue: 'pki.example.com'}]
).
Uses the DNS protocol to resolve CNAME
records for the hostname
. On success,\nthe Promise
is resolved with an array of canonical name records available for\nthe hostname
(e.g. ['bar.example.com']
).
Uses the DNS protocol to resolve mail exchange records (MX
records) for the\nhostname
. On success, the Promise
is resolved with an array of objects\ncontaining both a priority
and exchange
property (e.g.\n[{priority: 10, exchange: 'mx.example.com'}, ...]
).
Uses the DNS protocol to resolve regular expression based records (NAPTR
\nrecords) for the hostname
. On success, the Promise
is resolved with an array\nof objects with the following properties:
flags
service
regexp
replacement
order
preference
{\n flags: 's',\n service: 'SIP+D2U',\n regexp: '',\n replacement: '_sip._udp.example.com',\n order: 30,\n preference: 100\n}\n
"
},
{
"textRaw": "`dnsPromises.resolveNs(hostname)`",
"type": "method",
"name": "resolveNs",
"meta": {
"added": [
"v10.6.0"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`hostname` {string}",
"name": "hostname",
"type": "string"
}
]
}
],
"desc": "Uses the DNS protocol to resolve name server records (NS
records) for the\nhostname
. On success, the Promise
is resolved with an array of name server\nrecords available for hostname
(e.g.\n['ns1.example.com', 'ns2.example.com']
).
Uses the DNS protocol to resolve pointer records (PTR
records) for the\nhostname
. On success, the Promise
is resolved with an array of strings\ncontaining the reply records.
Uses the DNS protocol to resolve a start of authority record (SOA
record) for\nthe hostname
. On success, the Promise
is resolved with an object with the\nfollowing properties:
nsname
hostmaster
serial
refresh
retry
expire
minttl
{\n nsname: 'ns.example.com',\n hostmaster: 'root.example.com',\n serial: 2013101809,\n refresh: 10000,\n retry: 2400,\n expire: 604800,\n minttl: 3600\n}\n
"
},
{
"textRaw": "`dnsPromises.resolveSrv(hostname)`",
"type": "method",
"name": "resolveSrv",
"meta": {
"added": [
"v10.6.0"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`hostname` {string}",
"name": "hostname",
"type": "string"
}
]
}
],
"desc": "Uses the DNS protocol to resolve service records (SRV
records) for the\nhostname
. On success, the Promise
is resolved with an array of objects with\nthe following properties:
priority
weight
port
name
{\n priority: 10,\n weight: 5,\n port: 21223,\n name: 'service.example.com'\n}\n
"
},
{
"textRaw": "`dnsPromises.resolveTxt(hostname)`",
"type": "method",
"name": "resolveTxt",
"meta": {
"added": [
"v10.6.0"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`hostname` {string}",
"name": "hostname",
"type": "string"
}
]
}
],
"desc": "Uses the DNS protocol to resolve text queries (TXT
records) for the\nhostname
. On success, the Promise
is resolved with a two-dimensional array\nof 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.
Performs a reverse DNS query that resolves an IPv4 or IPv6 address to an\narray of host names.
\nOn error, the Promise
is rejected with an Error
object, where err.code
\nis one of the DNS error codes.
Set the default value of verbatim
in dns.lookup()
and\ndnsPromises.lookup()
. The value could be:
ipv4first
: sets default verbatim
false
.verbatim
: sets default verbatim
true
.The default is ipv4first
and dnsPromises.setDefaultResultOrder()
have\nhigher priority than --dns-result-order
. When using worker threads,\ndnsPromises.setDefaultResultOrder()
from the main thread won't affect the\ndefault dns orders in workers.
Sets the IP address and port of servers to be used when performing DNS\nresolution. The servers
argument is an array of RFC 5952 formatted\naddresses. If the port is the IANA default DNS port (53) it can be omitted.
dnsPromises.setServers([\n '4.4.4.4',\n '[2001:4860:4860::8888]',\n '4.4.4.4:1053',\n '[2001:4860:4860::8888]:1053',\n]);\n
\nAn error will be thrown if an invalid address is provided.
\nThe dnsPromises.setServers()
method must not be called while a DNS query is in\nprogress.
This method works much like\nresolve.conf.\nThat is, if attempting to resolve with the first server provided results in a\nNOTFOUND
error, the resolve()
method will not attempt to resolve with\nsubsequent servers provided. Fallback DNS servers will only be used if the\nearlier ones time out or result in some other error.
Each DNS query can return one of the following error codes:
\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 host name.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 host name 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.
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 changing these files will change the behavior of all other\nprograms running on the same operating system.
Though the call to dns.lookup()
will be asynchronous from JavaScript's\nperspective, it is implemented as a synchronous call to getaddrinfo(3)
that runs\non libuv's threadpool. This can have surprising negative performance\nimplications for some applications, see the UV_THREADPOOL_SIZE
\ndocumentation for more information.
Various networking APIs will call dns.lookup()
internally to resolve\nhost names. If that is an issue, consider resolving the host name to an address\nusing dns.resolve()
and using the address instead of a host name. Also, some\nnetworking APIs (such as socket.connect()
and dgram.createSocket()
)\nallow the default resolver, dns.lookup()
, to be replaced.
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.
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.
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
.