DNS resolution and connection failures
Prerequisites: previous curriculum lessons on Node streams, HTTP, buffers and asynchronous errors. Local lab baseline: Node.js 22.16.0, Linux, ESM. Use a disposable directory. Static examples are not ByteMind interactive execution. Each example is independent; save the named files together and run node main.mjs. The author harness imposes a five-second process deadline.
How it works
lookup can consult local hosts configuration and OS name services; resolve4 asks for DNS A records through the resolver. lookup can use the libuv pool for OS getaddrinfo. Use all: true when you need the full returned address list, and remember that address order and IPv6 availability depend on the environment. Error codes such as ENOTFOUND, EAI_AGAIN, ECONNREFUSED and ETIMEDOUT are diagnostic clues, not definitive root-cause proofs. Bound retries and distinguish a DNS error from a transport or application error. Do not log private hostnames or assume DNS answers stay fixed.
Look up a numeric loopback address without external DNS
import { lookup } from 'node:dns/promises';const addresses=await lookup('127.0.0.1',{all:true});console.log(addresses[0].address,addresses[0].family);Expected output:
127.0.0.1 4This proves the local API handles a numeric IPv4 address. It deliberately does not contact a DNS server.
Classify synthetic failure codes by stage
const cases=['ENOTFOUND','EAI_AGAIN','ECONNREFUSED','ETIMEDOUT','OTHER'];function category(code){ if(['ENOTFOUND','EAI_AGAIN'].includes(code))return 'resolution'; if(code==='ECONNREFUSED')return 'connection refused'; if(code==='ETIMEDOUT')return 'timeout: inspect stage'; return 'unknown: collect evidence';}for(const code of cases)console.log(code,category(code));Expected output:
ENOTFOUND resolutionEAI_AGAIN resolutionECONNREFUSED connection refusedETIMEDOUT timeout: inspect stageOTHER unknown: collect evidenceThis is an interpretation worksheet, not observed network failure telemetry.
Common mistakes
Do not replace a failed hostname with an arbitrary IP or disable TLS verification. Diagnose resolution, reachability and certificate identity separately. ENOTFOUND does not prove a domain never exists.
Practice
Add a synthetic timeout to the classification table. Explain which diagnostic evidence would distinguish a refused connection from an unreachable host.
Further reading
Official documentation: dns. Internal reference candidate: node-dns-resolution-and-connection-failures-lookup (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
DNS resolution and connection failures — knowledge check
Apply the lesson to a fresh scenario and explain the resource or protocol boundary.
Which API can use the operating system hosts configuration?
A name resolves but the connection is refused. What is justified?
What is a safe response to EAI_AGAIN?