@@ -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' ]);
@@ -262,21 +301,38 @@ time to consult the [Implementation considerations section][] before using
262
301
263
302
Example usage:
264
303
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
266
322
const dns = require (' node:dns' );
267
323
const options = {
268
324
family: 6 ,
269
325
hints: dns .ADDRCONFIG | dns .V4MAPPED ,
270
326
};
271
- dns .lookup (' example.com ' , options, (err , address , family ) =>
327
+ dns .lookup (' example.org ' , options, (err , address , family ) =>
272
328
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
274
330
275
331
// When options.all is true, the result will be an Array.
276
332
options .all = true ;
277
- dns .lookup (' example.com ' , options, (err , addresses ) =>
333
+ dns .lookup (' example.org ' , options, (err , addresses ) =>
278
334
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}]
280
336
```
281
337
282
338
If this method is invoked as its [ ` util.promisify() ` ] [ ] ed version, and ` all `
@@ -333,7 +389,15 @@ will be thrown.
333
389
334
390
On an error, ` err ` is an [ ` Error ` ] [ ] object, where ` err.code ` is the error code.
335
391
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
337
401
const dns = require (' node:dns' );
338
402
dns .lookupService (' 127.0.0.1' , 22 , (err , hostname , service ) => {
339
403
console .log (hostname, service);
@@ -904,7 +968,16 @@ the servers used for a resolver using
904
968
[ ` resolver.setServers() ` ] [ `dnsPromises.setServers()` ] does not affect
905
969
other resolvers:
906
970
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
908
981
const { Resolver } = require (' node:dns' ).promises ;
909
982
const resolver = new Resolver ();
910
983
resolver .setServers ([' 4.4.4.4' ]);
@@ -1036,24 +1109,45 @@ using `dnsPromises.lookup()`.
1036
1109
1037
1110
Example usage:
1038
1111
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
1040
1134
const dns = require (' node:dns' );
1041
1135
const dnsPromises = dns .promises ;
1042
1136
const options = {
1043
1137
family: 6 ,
1044
1138
hints: dns .ADDRCONFIG | dns .V4MAPPED ,
1045
1139
};
1046
1140
1047
- dnsPromises .lookup (' example.com ' , options).then ((result ) => {
1141
+ dnsPromises .lookup (' example.org ' , options).then ((result ) => {
1048
1142
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
1050
1144
});
1051
1145
1052
1146
// When options.all is true, the result will be an Array.
1053
1147
options .all = true ;
1054
- dnsPromises .lookup (' example.com ' , options).then ((result ) => {
1148
+ dnsPromises .lookup (' example.org ' , options).then ((result ) => {
1055
1149
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}]
1057
1151
});
1058
1152
```
1059
1153
@@ -1076,7 +1170,14 @@ will be thrown.
1076
1170
On error, the ` Promise ` is rejected with an [ ` Error ` ] [ ] object, where ` err.code `
1077
1171
is the error code.
1078
1172
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
1080
1181
const dnsPromises = require (' node:dns' ).promises ;
1081
1182
dnsPromises .lookupService (' 127.0.0.1' , 22 ).then ((result ) => {
1082
1183
console .log (result .hostname , result .service );
0 commit comments