KoderSolutionKoderSolution LogoKoderSolution Logo
HomeArticlesTutorialsToolsRun CodeForumAI Lab
🟢

Node.js

Topic Hub & Articles

Node.js Intro

10 min

Node.js Get Started

10 min

Node.js Modules

10 min

Node.js HTTP Module

10 min

Node.js File System

10 min

Node.js URL Module

10 min

Node.js NPM

10 min

Node.js Events

10 min

Node.js Upload Files

10 min

Node.js Email

10 min

Node.js Buffer

10 min

Recap Quiz

Gated

Milestone Assessment

Node.js Streams

10 min

Node.js Crypto

10 min

Node.js OS Module

10 min

Node.js Path Module

10 min

Node.js Global Objects

10 min

Recap Quiz

Gated

Milestone Assessment

Node.js Process

10 min

Node.js Child Processes

10 min

Node.js Worker Threads

10 min

Node.js DNS Module

10 min

Node.js Query String

10 min

Recap Quiz

Gated

Milestone Assessment

MySQL Connect

10 min

MySQL Create Database

10 min

Recap Quiz

Gated

Milestone Assessment

Node.js MySQL ORDER BY Clause

10 min

Recap Quiz

Gated

Milestone Assessment

MongoDB Intro

10 min

MongoDB Create Database

10 min

MongoDB Create Collection

10 min

MongoDB Insert

10 min

Recap Quiz

Gated

Milestone Assessment

MongoDB Find

10 min

MongoDB Query

10 min

MongoDB Sort

10 min

MongoDB Delete

10 min

MongoDB Update

10 min

Recap Quiz

Gated

Milestone Assessment

MongoDB Limit

10 min

MongoDB Join

10 min

Progress
0%

0 / 35 Lessons

KoderSolutionKoderSolution LogoKoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Facebook PageX (Twitter) ProfileYouTube ChannelContact Support

Engineering Newsletter

Subscribe for weekly software engineering tutorials, architecture guides, and developer tool updates.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
Node.jsNode.js Tutorial
Lesson

Node.js DNS Module

10 min reading
Free Course

Node.js DNS Module: Resolving Domains and DNS Records

The node:dns/promises module lets Node.js scripts look up IP addresses for domain names and inspect MX, TXT, CNAME, and A/AAAA records. Backend services use DNS lookups for email domain verification, service discovery, and validating external webhook URLs against local IP spoofing.

dns.lookup() vs dns.resolve4()

Node.js separates DNS resolution into two distinct families that behave very differently under load:

  • dns.lookup(hostname): Calls the operating system's resolver (reading /etc/hosts and OS DNS cache) inside Node's libuv thread pool. It matches what fetch() and curl see.
  • dns.resolve4(hostname) / dns.resolveMx(hostname): Sends live DNS packets over the network directly to DNS servers without touching /etc/hosts or consuming libuv threads.
import dns from 'node:dns/promises';

async function inspectDomain(domain) {
  const addresses = await dns.lookup(domain, { all: true });
  const mxRecords = await dns.resolveMx(domain);

  console.log('OS Lookup IPs:', addresses);
  console.log('Mail Servers:', mxRecords.sort((a, b) => a.priority - b.priority));
}

inspectDomain('example.com').catch((err) => console.error('DNS error:', err.message));

Guarding Webhooks Against SSRF via DNS Inspection

When your API accepts user-supplied webhook URLs, checking that the URL string says https://evil.example.com is not enough—the domain's DNS A record could point to 127.0.0.1 or 169.254.169.254 (cloud metadata). Resolving the IP first and rejecting private/loopback IP ranges closes that attack vector.

Common Mistakes to Avoid

  • Flooding dns.lookup() without realizing it uses the 4-thread libuv pool: Hundreds of slow dns.lookup() calls can stall file-system operations sharing the same thread pool. Use dns.resolve4() when querying external DNS records in bulk.
  • Calling dns.resolve('localhost'): dns.resolve* skips /etc/hosts and fails on local aliases like localhost or Docker container names. Use dns.lookup() when resolving local hostnames.
  • Assuming every domain has IPv6 (AAAA) records: Wrap dns.resolve6() in try / catch because many domains only publish IPv4 A records (ENODATA).

Frequently Asked Questions

Why does dns.resolveMx() return a priority number?

Lower priority numbers indicate the preferred mail server; mail clients try the lowest priority host first before falling back to higher numbers.

Does Node.js cache dns.lookup() results forever?

No. Node.js delegates dns.lookup() directly to the host OS resolver, whose caching behavior depends on Linux nscd/systemd-resolved, macOS, or Windows DNS cache settings.

How do I inspect SPF or domain-verification TXT records?

Call await dns.resolveTxt('example.com'), which returns an array of string arrays representing each DNS TXT record.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground

Related Developer Tools & Topic Hub

Node.js Topic Hub
JSON Formatter
Validate, format, and minify JSON
Regex Tester
Test regular expression patterns
JWT Decoder
Inspect JSON Web Token payloads
All Node.js Articles
Deep-dive guides, tips & discussions
PreviousNode.js Worker Threads
Continue JourneyNext: Node.js Query String

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum