@@ -15,13 +15,22 @@ facilities to perform name resolution. It may not need to perform any network |
15 | 15 | communication. To perform name resolution the way other applications on the same |
16 | 16 | system do, use [`dns.lookup()`][]. |
17 | 17 |
|
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 |
19 | 28 | const dns = require('node:dns'); |
20 | 29 |
|
21 | 30 | dns.lookup('example.org', (err, address, family) => { |
22 | 31 | console.log('address: %j family: IPv%s', address, family); |
23 | 32 | }); |
24 | | -// address: "93.184.216.34" family: IPv4 |
| 33 | +// address: "2606:2800:21f:cb07:6820:80da:af6b:8b2c" family: IPv6 |
25 | 34 | ``` |
26 | 35 |
|
27 | 36 | 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 |
30 | 39 | [`dns.lookup()`][] (e.g. `/etc/hosts`). Use these functions to always perform |
31 | 40 | DNS queries, bypassing other name-resolution facilities. |
32 | 41 |
|
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 |
34 | 62 | const dns = require('node:dns'); |
35 | 63 |
|
36 | 64 | dns.resolve4('archive.org', (err, addresses) => { |
@@ -64,7 +92,18 @@ the servers used for a resolver using |
64 | 92 | [`resolver.setServers()`][`dns.setServers()`] does not affect |
65 | 93 | other resolvers: |
66 | 94 |
|
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 |
68 | 107 | const { Resolver } = require('node:dns'); |
69 | 108 | const resolver = new Resolver(); |
70 | 109 | resolver.setServers(['4.4.4.4']); |
@@ -260,21 +299,38 @@ time to consult the [Implementation considerations section][] before using |
260 | 299 |
|
261 | 300 | Example usage: |
262 | 301 |
|
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 |
264 | 320 | const dns = require('node:dns'); |
265 | 321 | const options = { |
266 | 322 | family: 6, |
267 | 323 | hints: dns.ADDRCONFIG | dns.V4MAPPED, |
268 | 324 | }; |
269 | | -dns.lookup('example.com', options, (err, address, family) => |
| 325 | +dns.lookup('example.org', options, (err, address, family) => |
270 | 326 | 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 |
272 | 328 |
|
273 | 329 | // When options.all is true, the result will be an Array. |
274 | 330 | options.all = true; |
275 | | -dns.lookup('example.com', options, (err, addresses) => |
| 331 | +dns.lookup('example.org', options, (err, addresses) => |
276 | 332 | 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}] |
278 | 334 | ``` |
279 | 335 |
|
280 | 336 | If this method is invoked as its [`util.promisify()`][]ed version, and `all` |
@@ -331,7 +387,15 @@ will be thrown. |
331 | 387 |
|
332 | 388 | On an error, `err` is an [`Error`][] object, where `err.code` is the error code. |
333 | 389 |
|
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 |
335 | 399 | const dns = require('node:dns'); |
336 | 400 | dns.lookupService('127.0.0.1', 22, (err, hostname, service) => { |
337 | 401 | console.log(hostname, service); |
@@ -898,7 +962,16 @@ the servers used for a resolver using |
898 | 962 | [`resolver.setServers()`][`dnsPromises.setServers()`] does not affect |
899 | 963 | other resolvers: |
900 | 964 |
|
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 |
902 | 975 | const { Resolver } = require('node:dns').promises; |
903 | 976 | const resolver = new Resolver(); |
904 | 977 | resolver.setServers(['4.4.4.4']); |
@@ -1028,24 +1101,45 @@ using `dnsPromises.lookup()`. |
1028 | 1101 |
|
1029 | 1102 | Example usage: |
1030 | 1103 |
|
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 |
1032 | 1126 | const dns = require('node:dns'); |
1033 | 1127 | const dnsPromises = dns.promises; |
1034 | 1128 | const options = { |
1035 | 1129 | family: 6, |
1036 | 1130 | hints: dns.ADDRCONFIG | dns.V4MAPPED, |
1037 | 1131 | }; |
1038 | 1132 |
|
1039 | | -dnsPromises.lookup('example.com', options).then((result) => { |
| 1133 | +dnsPromises.lookup('example.org', options).then((result) => { |
1040 | 1134 | 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 |
1042 | 1136 | }); |
1043 | 1137 |
|
1044 | 1138 | // When options.all is true, the result will be an Array. |
1045 | 1139 | options.all = true; |
1046 | | -dnsPromises.lookup('example.com', options).then((result) => { |
| 1140 | +dnsPromises.lookup('example.org', options).then((result) => { |
1047 | 1141 | 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}] |
1049 | 1143 | }); |
1050 | 1144 | ``` |
1051 | 1145 |
|
@@ -1068,7 +1162,14 @@ will be thrown. |
1068 | 1162 | On error, the `Promise` is rejected with an [`Error`][] object, where `err.code` |
1069 | 1163 | is the error code. |
1070 | 1164 |
|
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 |
1072 | 1173 | const dnsPromises = require('node:dns').promises; |
1073 | 1174 | dnsPromises.lookupService('127.0.0.1', 22).then((result) => { |
1074 | 1175 | console.log(result.hostname, result.service); |
|