Question presented to candidate: "dns.lookup('localhost') and dns.resolve4('localhost') sound like they should do the same thing. Do they actually use the same underlying mechanism, and would you expect identical results?"
What a strong answer should cover:
- A browser has no direct DNS-resolution API exposed to JavaScript at all — DNS resolution happens entirely inside the browser/OS network stack, invisibly, as part of making a request; there is no equivalent to Node's
dnsmodule callable from browser JS. - Node's
dnsmodule exposes two genuinely different code paths, not just two names for the same operation:dns.lookup()uses the operating system's own resolver (viagetaddrinfo, dispatched through libuv's thread pool, covered in its own dedicated question) — this respects the OS-level hosts file and its configured resolution order. - 📌 Verified, not assumed:
dns.resolve()/dns.resolve4()/etc. use a different implementation entirely (thec-areslibrary), which queries a DNS server directly over the network, bypassing the OS's hosts-file-aware resolution — confirmed directly:dns.lookup("localhost")returned::1(an OS-resolved loopback address), whiledns.resolve4against a real public domain returned genuine, live public IP addresses fetched directly from DNS servers. - This distinction has real practical consequences:
dns.lookup()will correctly resolve an entry that only exists in a local hosts file (or via other OS-level resolution mechanisms like mDNS);dns.resolve()'s family of functions will not see that entry at all, since they never consult the hosts file — they go straight to a DNS server. dns.lookup(), being dispatched through the thread pool, is subject to the thread pool's fixed size and its associated contention (covered fully in the dedicated thread-pool question) — a high volume of concurrentdns.lookup()calls can compete with other thread-pool-bound work (file I/O, some crypto).- A precise answer names when each is the right choice:
dns.lookup()for "resolve a hostname exactly the way any other program on this OS would" (including hosts-file entries);dns.resolve()'s family for "query DNS records directly and get back exactly what a DNS server returns" (useful for inspecting specific record types likeMX/TXT, whichlookup()does not expose at all).
Clarifying questions expected:
- "Does the resolution need to respect the local hosts file, or query DNS servers directly?" — the deciding factor between the two.
- "Is a specific DNS record type (MX, TXT, CNAME) actually needed, beyond just an IP address?" — only
dns.resolve()'s family exposes those.
Code / implementation expected: Yes — actually running dns.lookup and dns.resolve4 and observing genuinely different results/behavior is the concrete, convincing demonstration that these are different mechanisms, not just different function names.