bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/Node.js/Core Modules
Node.js•Core Modules

Node.js DNS Module

Concept visual

Node.js DNS Module

Pointer walk
two pointers
leftright102132436485116
left=0
right=6
1
3

Start at both ends

Introduction to the DNS Module

The DNS (Domain Name System) module provides functionality for name resolution in Node.js.

It offers two main APIs:

Callback-based API

: Traditional Node.js style with callback functions

Promise-based API

Formula

: Modern async/await support via dns.promises

Key features include:

Formula

Resolving domain names to IP addresses (A/AAAA records)

Performing reverse DNS lookups (PTR records) Querying various DNS record types (MX, TXT, SRV, etc.)

Creating custom DNS resolvers with specific settings

Configuring DNS server settings programmatically

Note:

The DNS module can operate in two distinct modes - using the operating system's facilities or performing direct network DNS queries. This affects how hostname resolution works in your application.

Getting Started with DNS

Here's a quick example of using the DNS module to look up a domain's IP address:

Basic DNS Lookup const dns = require('dns');
// Look up a domain name dns.lookup('example.com', (err, address, family) => {
if (err) {
console.error('Lookup error:', err);
return;
}
console.log(`IP address: ${address}`);
console.log(`IP version: IPv${family}`);
});

Importing and Setup

Formula

To use the DNS module, you can import it in your Node.js application using either the callback or promise - based API:

Callback-based API

// Import the DNS module const dns = require('dns');
// Example usage dns.lookup('example.com', (err, address, family) => {
if (err) throw err;
console.log(`Resolved: ${address} (IPv${family})`);
});

Formula

Promise - based API (Node.js 10.0.0 +)

// Import the promises API

const { promises: dns } = require('dns');

// Or:

const dns = require('dns').promises;
// Example with async/await async function lookupDomain(domain) {
try {
const address = await dns.lookup(domain);
console.log(`Resolved: ${address.address} (IPv${address.family})`);
} catch (err) {
console.error('Lookup failed:', err);
}
}
lookupDomain('example.com');

Note:

The promise-based API is generally preferred for new code as it works better with modern async/await patterns and provides better error handling.

Basic DNS Lookups

The DNS module provides several methods for looking up domain names and IP addresses. The most common operations are: dns.lookup()

Formula

: Uses the operating system's facilities to resolve hostnames dns.resolve*()

: Performs DNS queries directly to name servers dns.reverse() : Performs reverse DNS lookups (IP to hostname)

Resolving Domain Names to IP Addresses const dns = require('dns');

Formula

// Callback - based API
dns.lookup('www.example.com', (err, address, family) => {
if (err) throw err;
console.log('IP address: %s', address);
console.log('IP version: IPv%s', family);
});
const dns = require('dns').promises;

Formula

// Promise - based API
async function lookupExample() {
try {
const result = await dns.lookup('www.example.com');
console.log('IP address:', result.address);
console.log('IP version: IPv' + result.family);
} catch (err) {
console.error('Lookup failed:', err);
}
}
lookupExample();

Note:

The dns.lookup() method uses the operating system's facilities for name resolution and does not necessarily perform any network communication.

Looking Up All IP Addresses for a Domain const dns = require('dns');
// Get all IPv4 addresses dns.resolve4('www.google.com', (err, addresses) => {
if (err) throw err;
console.log('IPv4 addresses:');
addresses.forEach(address => {
console.log(`  ${address}`);
});

// Perform a reverse lookup on the first IP

dns.reverse(addresses[0], (err, hostnames) => {
if (err) throw err;
console.log(`Reverse lookup for ${addresses[0]}:`);
hostnames.forEach(hostname => {
console.log(`  ${hostname}`);
});
});
});

DNS Record Types

The DNS module supports lookups for various DNS record types:

Method

Record Type

Description resolve4()

A

IPv4 addresses resolve6()

Aaaa

IPv6 addresses resolveMx()

MX

Mail exchange records resolveTxt()

Txt

Text records resolveSrv()

Previous

Node.js Timers Module

Next

Node.js Assert Module