Skip to content

Commit 62bf03b

Browse files
mfdebiantargos
authored andcommitted
doc: add esm examples to node:dns
PR-URL: #54172 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent dd8ab83 commit 62bf03b

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']);
@@ -260,21 +299,38 @@ time to consult the [Implementation considerations section][] before using
260299

261300
Example usage:
262301

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

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

280336
If this method is invoked as its [`util.promisify()`][]ed version, and `all`
@@ -331,7 +387,15 @@ will be thrown.
331387

332388
On an error, `err` is an [`Error`][] object, where `err.code` is the error code.
333389

334-
```js
390+
```mjs
391+
import dns from 'node:dns';
392+
dns.lookupService('127.0.0.1', 22, (err, hostname, service) => {
393+
console.log(hostname, service);
394+
// Prints: localhost ssh
395+
});
396+
```
397+
398+
```cjs
335399
const dns = require('node:dns');
336400
dns.lookupService('127.0.0.1', 22, (err, hostname, service) => {
337401
console.log(hostname, service);
@@ -898,7 +962,16 @@ the servers used for a resolver using
898962
[`resolver.setServers()`][`dnsPromises.setServers()`] does not affect
899963
other resolvers:
900964

901-
```js
965+
```mjs
966+
import { Resolver } from 'node:dns/promises';
967+
const resolver = new Resolver();
968+
resolver.setServers(['4.4.4.4']);
969+
970+
// This request will use the server at 4.4.4.4, independent of global settings.
971+
const addresses = await resolver.resolve4('example.org');
972+
```
973+
974+
```cjs
902975
const { Resolver } = require('node:dns').promises;
903976
const resolver = new Resolver();
904977
resolver.setServers(['4.4.4.4']);
@@ -1028,24 +1101,45 @@ using `dnsPromises.lookup()`.
10281101

10291102
Example usage:
10301103

1031-
```js
1104+
```mjs
1105+
import dns from 'node:dns';
1106+
const dnsPromises = dns.promises;
1107+
const options = {
1108+
family: 6,
1109+
hints: dns.ADDRCONFIG | dns.V4MAPPED,
1110+
};
1111+
1112+
await dnsPromises.lookup('example.org', options).then((result) => {
1113+
console.log('address: %j family: IPv%s', result.address, result.family);
1114+
// address: "2606:2800:21f:cb07:6820:80da:af6b:8b2c" family: IPv6
1115+
});
1116+
1117+
// When options.all is true, the result will be an Array.
1118+
options.all = true;
1119+
await dnsPromises.lookup('example.org', options).then((result) => {
1120+
console.log('addresses: %j', result);
1121+
// addresses: [{"address":"2606:2800:21f:cb07:6820:80da:af6b:8b2c","family":6}]
1122+
});
1123+
```
1124+
1125+
```cjs
10321126
const dns = require('node:dns');
10331127
const dnsPromises = dns.promises;
10341128
const options = {
10351129
family: 6,
10361130
hints: dns.ADDRCONFIG | dns.V4MAPPED,
10371131
};
10381132

1039-
dnsPromises.lookup('example.com', options).then((result) => {
1133+
dnsPromises.lookup('example.org', options).then((result) => {
10401134
console.log('address: %j family: IPv%s', result.address, result.family);
1041-
// address: "2606:2800:220:1:248:1893:25c8:1946" family: IPv6
1135+
// address: "2606:2800:21f:cb07:6820:80da:af6b:8b2c" family: IPv6
10421136
});
10431137

10441138
// When options.all is true, the result will be an Array.
10451139
options.all = true;
1046-
dnsPromises.lookup('example.com', options).then((result) => {
1140+
dnsPromises.lookup('example.org', options).then((result) => {
10471141
console.log('addresses: %j', result);
1048-
// addresses: [{"address":"2606:2800:220:1:248:1893:25c8:1946","family":6}]
1142+
// addresses: [{"address":"2606:2800:21f:cb07:6820:80da:af6b:8b2c","family":6}]
10491143
});
10501144
```
10511145

@@ -1068,7 +1162,14 @@ will be thrown.
10681162
On error, the `Promise` is rejected with an [`Error`][] object, where `err.code`
10691163
is the error code.
10701164

1071-
```js
1165+
```mjs
1166+
import dnsPromises from 'node:dns/promises';
1167+
const result = await dnsPromises.lookupService('127.0.0.1', 22);
1168+
1169+
console.log(result.hostname, result.service); // Prints: localhost ssh
1170+
```
1171+
1172+
```cjs
10721173
const dnsPromises = require('node:dns').promises;
10731174
dnsPromises.lookupService('127.0.0.1', 22).then((result) => {
10741175
console.log(result.hostname, result.service);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy