How Node really works · Chapter 20 of 34

DNS

By the end of this chapter you can turn names into addresses the right way, read mail records, test DNS code with no internet, and cache answers safely. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Networking
  • 5 tasks
  • Node.js
  • In the all labs plan

The first 2 chapters of How Node really works are free. This one comes with the all labs plan.

Why you need this

Ravi has moved the kitchen printer to its own machine with a name, so the order server can connect by name and nobody edits code when its IP changes. Anjali wants receipts emailed, and the email service needs the mail servers of a customer's domain before it sends anything.

Both are DNS, and Node has two very different ways of asking. Pick the wrong one and a name in the hosts file is not found, or file reads slow down for no visible reason. And the lab has no internet, so code that only works against real DNS cannot even be tested here.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    kitchenAddress: find the IPv4 address of the kitchen machine

    Write kitchenAddress(hostname). It finds the address the way the operating system does (the hosts file included) and returns a promise of the IPv4 address alone, as a string. kitchenAddress('localhost') must give '127.0.0.1', even though a plain lookup of localhost gives '::1' first in the lab.

  2. Task 2

    mailHosts: list a domain's mail servers in priority order

    Write mailHosts(domain, resolveMx), where resolveMx is dns.resolveMx when left out. Call resolveMx(domain): it resolves with records { exchange, priority }. Return a promise of the exchange names alone, lowest priority number first.

    If resolveMx rejects with code 'ENOTFOUND' (no such domain) or 'ENODATA' (no mail records), return []. Any other error, such as 'ETIMEOUT', must reject with that same error.

  3. Task 3

    createCachedLookup: cache lookups for a while instead of asking every time

    Write createCachedLookup(resolve, ttlMs), with ttlMs 30000 when left out. resolve is a function from a hostname to a promise of an answer. Return a function lookup(hostname) that returns a promise of the answer: the first call for a hostname asks resolve, and later calls reuse that answer, without asking, until ttlMs milliseconds have passed since it was asked (at exactly ttlMs, ask again). Each hostname has its own entry.

    Calls that arrive while a query is still running share it: two calls at once ask only once. A failed query is not cached: the call rejects with its error, and the next call asks again.

  4. Task 4

    makeResolver: point a resolver at Tapri's own DNS servers

    Write makeResolver(servers, timeoutMs), with timeoutMs 2000 when left out. It returns a new Resolver from node:dns/promises that waits at most timeoutMs for each query, tries once, and asks only the servers you are given, in that order. Each call makes a new resolver, and Node's default servers must stay as they were.

  5. Task 5 · interview question

    Interview: lookupOrResolve, say which of lookup and resolve does what

    Fill in lookupOrResolve. For each of its six statements, write 'lookup' (dns.lookup) or 'resolve' (resolve4, resolveMx and the other resolve functions), whichever the statement is true of. Keep the six keys as they are.

What you will learn

  • dns.lookup: asking the operating system, the way fetch does.
  • The resolve functions: asking a DNS server directly, for any record type.
  • Resolvers you can swap: your own servers, and fakes in tests.
  • Caching: reusing answers for a while, without the classic bugs.

More in Networking

Build "DNS" yourself

Tapri’s order server must never stall at the 4 pm rush. You learn why it would. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of How Node really works are free. This one comes with the all labs plan.