Skip to content

Commit e2c6220

Browse files
authored
doc: add esm examples to node:dns
PR-URL: #54172 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Ulises Gascón <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent dc86592 commit e2c6220

File tree

1 file changed

+118
-17
lines changed

1 file changed

+118
-17
lines changed

doc/api/dns.md

Lines changed: 118 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,22 @@ facilities to perform name resolution. It may not need to perform any network
1515
communication. To perform name resolution the way other applications on the same
1616
system do, use [`dns.lookup()`][].
1717

18-
```js
18+
```mjs
19+
import dns from 'node:dns';
20+
21+
dns.lookup('example.org', (err, address, family) => {
22+
console.log('address: %j family: IPv%s', address, family);
23+
});
24+
// address: "2606:2800:21f:cb07:6820:80da:af6b:8b2c" family: IPv6
25+
```
26+
27+
```cjs
1928
const dns = require('node:dns');
2029

2130
dns.lookup('example.org', (err, address, family) => {
2231
console.log('address: %j family: IPv%s', address, family);
2332
});
24-
// address: "93.184.216.34" family: IPv4
33+
// address: "2606:2800:21f:cb07:6820:80da:af6b:8b2c" family: IPv6
2534
```
2635

2736
All other functions in the `node:dns` module connect to an actual DNS server to
@@ -30,7 +39,26 @@ queries. These functions do not use the same set of configuration files used by
3039
[`dns.lookup()`][] (e.g. `/etc/hosts`). Use these functions to always perform
3140
DNS queries, bypassing other name-resolution facilities.
3241

33-
```js
42+
```mjs
43+
import dns from 'node:dns';
44+
45+
dns.resolve4('archive.org', (err, addresses) => {
46+
if (err) throw err;
47+
48+
console.log(`addresses: ${JSON.stringify(addresses)}`);
49+
50+
addresses.forEach((a) => {
51+
dns.reverse(a, (err, hostnames) => {
52+
if (err) {
53+
throw err;
54+
}
55+
console.log(`reverse for ${a}: ${JSON.stringify(hostnames)}`);
56+
});
57+
});
58+
});
59+
```
60+
61+
```cjs
3462
const dns = require('node:dns');
3563

3664
dns.resolve4('archive.org', (err, addresses) => {
@@ -64,7 +92,18 @@ the servers used for a resolver using
6492
[`resolver.setServers()`][`dns.setServers()`] does not affect
6593
other resolvers:
6694

67-
```js
95+
```mjs
96+
import { Resolver } from 'node:dns';
97+
const resolver = new Resolver();
98+
resolver.setServers(['4.4.4.4']);
99+
100+
// This request will use the server at 4.4.4.4, independent of global settings.
101+
resolver.resolve4('example.org', (err, addresses) => {
102+
// ...
103+
});
104+
```
105+
106+
```cjs
68107
const { Resolver } = require('node:dns');
69108
const resolver = new Resolver();
70109
resolver.setServers(['4.4.4.4']);
@@ -262,21 +301,38 @@ time to consult the [Implementation considerations section][] before using
262301

263302
Example usage:
264303

265-
```js
304+
```mjs
305+
import dns from 'node:dns';
306+
const options = {
307+
family: 6,
308+
hints: dns.ADDRCONFIG | dns.V4MAPPED,
309+
};
310+
dns.lookup('example.org', options, (err, address, family) =>
311+
console.log('address: %j family: IPv%s', address, family));
312+
// address: "2606:2800:21f:cb07:6820:80da:af6b:8b2c" family: IPv6
313+
314+
// When options.all is true, the result will be an Array.
315+
options.all = true;
316+
dns.lookup('example.org', options, (err, addresses) =>
317+
console.log('addresses: %j', addresses));
318+
// addresses: [{"address":"2606:2800:21f:cb07:6820:80da:af6b:8b2c","family":6}]
319+
```
320+
321+
```cjs
266322
const dns = require('node:dns');
267323
const options = {
268324
family: 6,
269325
hints: dns.ADDRCONFIG | dns.V4MAPPED,
270326
};
271-
dns.lookup('example.com', options, (err, address, family) =>
327+
dns.lookup('example.org', options, (err, address, family) =>
272328
console.log('address: %j family: IPv%s', address, family));
273-
// address: "2606:2800:220:1:248:1893:25c8:1946" family: IPv6
329+
// address: "2606:2800:21f:cb07:6820:80da:af6b:8b2c" family: IPv6
274330

275331
// When options.all is true, the result will be an Array.
276332
options.all = true;
277-
dns.lookup('example.com', options, (err, addresses) =>
333+
dns.lookup('example.org', options, (err, addresses) =>
278334
console.log('addresses: %j', addresses));
279-
// addresses: [{"address":"2606:2800:220:1:248:1893:25c8:1946","family":6}]
335+
// addresses: [{"address":"2606:2800:21f:cb07:6820:80da:af6b:8b2c","family":6}]
280336
```
281337

282338
If this method is invoked as its [`util.promisify()`][]ed version, and `all`
@@ -333,7 +389,15 @@ will be thrown.
333389

334390
On an error, `err` is an [`Error`][] object, where `err.code` is the error code.
335391

336-
```js
392+
```mjs
393+
import dns from 'node:dns';
394+
dns.lookupService('127.0.0.1', 22, (err, hostname, service) => {
395+
console.log(hostname, service);
396+
// Prints: localhost ssh
397+
});
398+
```
399+
400+
```cjs
337401
const dns = require('node:dns');
338402
dns.lookupService('127.0.0.1', 22, (err, hostname, service) => {
339403
console.log(hostname, service);
@@ -904,7 +968,16 @@ the servers used for a resolver using
904968
[`resolver.setServers()`][`dnsPromises.setServers()`] does not affect
905969
other resolvers:
906970

907-
```js
971+
```mjs
972+
import { Resolver } from 'node:dns/promises';
973+
const resolver = new Resolver();
974+
resolver.setServers(['4.4.4.4']);
975+
976+
// This request will use the server at 4.4.4.4, independent of global settings.
977+
const addresses = await resolver.resolve4('example.org');
978+
```
979+
980+
```cjs
908981
const { Resolver } = require('node:dns').promises;
909982
const resolver = new Resolver();
910983
resolver.setServers(['4.4.4.4']);
@@ -1036,24 +1109,45 @@ using `dnsPromises.lookup()`.
10361109

10371110
Example usage:
10381111

1039-
```js
1112+
```mjs
1113+
import dns from 'node:dns';
1114+
const dnsPromises = dns.promises;
1115+
const options = {
1116+
family: 6,
1117+
hints: dns.ADDRCONFIG | dns.V4MAPPED,
1118+
};
1119+
1120+
await dnsPromises.lookup('example.org', options).then((result) => {
1121+
console.log('address: %j family: IPv%s', result.address, result.family);
1122+
// address: "2606:2800:21f:cb07:6820:80da:af6b:8b2c" family: IPv6
1123+
});
1124+
1125+
// When options.all is true, the result will be an Array.
1126+
options.all = true;
1127+
await dnsPromises.lookup('example.org', options).then((result) => {
1128+
console.log('addresses: %j', result);
1129+
// addresses: [{"address":"2606:2800:21f:cb07:6820:80da:af6b:8b2c","family":6}]
1130+
});
1131+
```
1132+
1133+
```cjs
10401134
const dns = require('node:dns');
10411135
const dnsPromises = dns.promises;
10421136
const options = {
10431137
family: 6,
10441138
hints: dns.ADDRCONFIG | dns.V4MAPPED,
10451139
};
10461140

1047-
dnsPromises.lookup('example.com', options).then((result) => {
1141+
dnsPromises.lookup('example.org', options).then((result) => {
10481142
console.log('address: %j family: IPv%s', result.address, result.family);
1049-
// address: "2606:2800:220:1:248:1893:25c8:1946" family: IPv6
1143+
// address: "2606:2800:21f:cb07:6820:80da:af6b:8b2c" family: IPv6
10501144
});
10511145

10521146
// When options.all is true, the result will be an Array.
10531147
options.all = true;
1054-
dnsPromises.lookup('example.com', options).then((result) => {
1148+
dnsPromises.lookup('example.org', options).then((result) => {
10551149
console.log('addresses: %j', result);
1056-
// addresses: [{"address":"2606:2800:220:1:248:1893:25c8:1946","family":6}]
1150+
// addresses: [{"address":"2606:2800:21f:cb07:6820:80da:af6b:8b2c","family":6}]
10571151
});
10581152
```
10591153

@@ -1076,7 +1170,14 @@ will be thrown.
10761170
On error, the `Promise` is rejected with an [`Error`][] object, where `err.code`
10771171
is the error code.
10781172

1079-
```js
1173+
```mjs
1174+
import dnsPromises from 'node:dns/promises';
1175+
const result = await dnsPromises.lookupService('127.0.0.1', 22);
1176+
1177+
console.log(result.hostname, result.service); // Prints: localhost ssh
1178+
```
1179+
1180+
```cjs
10801181
const dnsPromises = require('node:dns').promises;
10811182
dnsPromises.lookupService('127.0.0.1', 22).then((result) => {
10821183
console.log(result.hostname, result.service);

0 commit comments

Comments
 (0)