An independent resolver for DNS requests.

Creating a new resolver uses the default server settings. Setting the servers used for a resolver using resolver.setServers() does not affect other resolvers:

import { Resolver } from 'node:dns';
const resolver = new Resolver();
resolver.setServers(['4.4.4.4']);

// This request will use the server at 4.4.4.4, independent of global settings.
resolver.resolve4('example.org', (err, addresses) => {
// ...
});

The following methods from the node: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()

v8.3.0

Constructors

Properties

getServers: (() => string[])

Type declaration

    • (): string[]
    • Returns an array of IP address strings, formatted according to RFC 5952, that are currently configured for DNS resolution. A string will include a port section if a custom port is used.

      [
      '4.4.4.4',
      '2001:4860:4860::8888',
      '4.4.4.4:1053',
      '[2001:4860:4860::8888]:1053',
      ]

      Returns string[]

      v0.11.3

resolve: typeof resolve
resolve4: typeof resolve4
resolve6: typeof resolve6
resolveAny: typeof resolveAny
resolveCaa: typeof resolveCaa
resolveCname: typeof resolveCname
resolveMx: typeof resolveMx
resolveNaptr: typeof resolveNaptr
resolveNs: typeof resolveNs
resolvePtr: typeof resolvePtr
resolveSoa: typeof resolveSoa
resolveSrv: typeof resolveSrv
resolveTlsa: typeof resolveTlsa
resolveTxt: typeof resolveTxt
reverse: ((ip: string, callback: ((err: null | ErrnoException, hostnames: string[]) => void)) => void)

Type declaration

    • (ip, callback): void
    • Performs a reverse DNS query that resolves an IPv4 or IPv6 address to an array of host names.

      On error, err is an Error object, where err.code is one of the DNS error codes.

      Parameters

      • ip: string
      • callback: ((err: null | ErrnoException, hostnames: string[]) => void)
          • (err, hostnames): void
          • Parameters

            Returns void

      Returns void

      v0.1.16

setServers: ((servers: readonly string[]) => void)

Type declaration

    • (servers): void
    • Sets the IP address and port of servers to be used when performing DNS resolution. The servers argument is an array of RFC 5952 formatted addresses. If the port is the IANA default DNS port (53) it can be omitted.

      dns.setServers([
      '4.4.4.4',
      '[2001:4860:4860::8888]',
      '4.4.4.4:1053',
      '[2001:4860:4860::8888]:1053',
      ]);

      An error will be thrown if an invalid address is provided.

      The dns.setServers() method must not be called while a DNS query is in progress.

      The setServers method affects only resolve, dns.resolve*() and reverse (and specifically not lookup).

      This method works much like resolve.conf. That is, if attempting to resolve with the first server provided results in a NOTFOUND error, the resolve() method will not attempt to resolve with subsequent servers provided. Fallback DNS servers will only be used if the earlier ones time out or result in some other error.

      Parameters

      • servers: readonly string[]

        array of RFC 5952 formatted addresses

      Returns void

      v0.11.3

Methods

  • Cancel all outstanding DNS queries made by this resolver. The corresponding callbacks will be called with an error with code ECANCELLED.

    Returns void

    v8.3.0

  • The resolver instance will send its requests from the specified IP address. This allows programs to specify outbound interfaces when used on multi-homed systems.

    If a v4 or v6 address is not specified, it is set to the default and the operating system will choose a local address automatically.

    The resolver will use the v4 local address when making requests to IPv4 DNS servers, and the v6 local address when making requests to IPv6 DNS servers. The rrtype of resolution requests has no impact on the local address used.

    Parameters

    • Optionalipv4: string

      A string representation of an IPv4 address.

    • Optionalipv6: string

      A string representation of an IPv6 address.

    Returns void

    v15.1.0, v14.17.0