DNS over HTTP Resolver

3.0.16 · active · verified Tue Apr 21

This package provides an isomorphic (browser and Node.js) DNS over HTTP (DoH) resolver, leveraging the `fetch` API for network requests. It offers an API interface designed to be compatible with Node.js's built-in `dns.promises` API, simplifying migration or usage in environments where a native DNS resolver is desired but not available or insufficient. The current stable version is 3.0.16, with releases occurring roughly monthly or bi-monthly, primarily for dependency updates and minor improvements. Key differentiators include its `fetch`-based approach, built-in caching (configurable `maxCache`), and the ability to specify custom DoH servers, making it flexible for various privacy and performance requirements beyond the default Cloudflare and Google servers. It ships with TypeScript types, enhancing developer experience and type safety.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to import and instantiate the `DnsOverHttpResolver`, perform various DNS record lookups (A, TXT, MX), configure built-in caching, and dynamically update the list of DNS over HTTP servers for customized resolution.

import { DnsOverHttpResolver } from 'dns-over-http-resolver';

async function resolveDnsRecords() {
  const resolver = new DnsOverHttpResolver({
    maxCache: 200 // Configure cache size; default is 100
  });

  try {
    // Resolve A records (IPv4 addresses)
    console.log('Resolving A records for example.com...');
    const ipv4Addresses = await resolver.resolve4('example.com');
    console.log('IPv4 Addresses:', ipv4Addresses);

    // Resolve TXT records
    console.log('\nResolving TXT records for google.com...');
    const txtRecords = await resolver.resolveTxt('google.com');
    console.log('TXT Records:', txtRecords);

    // Resolve any record type (e.g., MX records for a mail server)
    console.log('\nResolving MX records for gmail.com...');
    const mxRecords = await resolver.resolve('gmail.com', 'MX');
    console.log('MX Records:', mxRecords);

    // Get current servers and set custom DNS over HTTP servers
    console.log('\nDefault servers:', resolver.getServers());
    resolver.setServers([
      'https://dns.quad9.net/dns-query', // Example: Quad9
      'https://dns.opendns.com/dns-query' // Example: OpenDNS
    ]);
    console.log('Updated servers:', resolver.getServers());

    // Resolve another domain with the newly configured servers
    const newIpv4 = await resolver.resolve4('cloudflare.com');
    console.log('IPv4 for cloudflare.com (via custom servers):', newIpv4);

  } catch (error) {
    console.error('DNS resolution failed:', error);
  }
}

resolveDnsRecords().catch(err => {
  console.error('An unhandled error occurred during quickstart execution:', err);
});

view raw JSON →