bhylu.com

Advertisements

Node.js DNS

Name lookup can involve operating-system configuration as well as DNS. Node’s lookup and resolve interfaces do not always ask the same underlying service.

How it works

lookup follows the host’s address-resolution facilities, which can include a local hosts file. resolve methods query DNS records through a different interface. The fixture checks localhost through lookup with all results requested and prints only whether a loopback address was present. It performs no query to a public DNS service and does not claim that the same address ordering or IPv4/IPv6 preference applies on every machine.

Run the code

Run this example locally with Node.js. The exact sources are reviewed on Node 22.15.1 and Node 24.22.1 unless a specialised lab states otherwise. Save the complete source as dns.mjs in a separate empty exercises folder and run node dns.mjs. There is no Node.js browser runner on Bhylu; the plain JavaScript playground does not provide these filesystem, server or process APIs.

Download the complete source. Ordinary Node scripts need no Apache, PHP or MySQL service. File examples own their temporary folders; network fixtures use loopback and available temporary ports.

Example

dns.mjs

import { lookup } from 'node:dns/promises';
const records = await lookup('localhost', { all: true });
console.log(
  records.some((record) => record.address === '127.0.0.1' || record.address === '::1'),
);
console.log(records.every((record) => [4, 6].includes(record.family)));

Expected result

true
true

The local host name resolves to an appropriate loopback result on the reviewed system. A server that fetches user-selected URLs needs a stronger destination policy than checking one textual hostname, especially when addresses can change or redirects are involved.

Use the Node.js API reference for the precise version contract.