|
4 | 4 |
|
5 | 5 | > Stability: 2 - Stable |
6 | 6 |
|
7 | | -The `dns` module contains functions belonging to two different categories: |
| 7 | +The `dns` module enables name resolution. For example, use it to look up IP |
| 8 | +addresses of host names. |
8 | 9 |
|
9 | | -1. Functions that use the underlying operating system facilities to perform |
10 | | - name resolution, and that do not necessarily perform any network |
11 | | - communication. |
12 | | - This category contains only one function: [`dns.lookup()`][]. **Developers |
13 | | - looking to perform name resolution in the same way that other applications |
14 | | - on the same operating system behave should use [`dns.lookup()`][].** |
| 10 | +Although named for the Domain Name System (DNS), it does not always use the DNS |
| 11 | +protocol for lookups. [`dns.lookup()`][] uses the operating system facilities to |
| 12 | +perform name resolution. It may not need to perform any network communication. |
| 13 | +Developers looking to perform name resolution in the same way that other |
| 14 | +applications on the same operating system behave should use [`dns.lookup()`][]. |
15 | 15 |
|
16 | | - For example, looking up `iana.org`. |
| 16 | +```js |
| 17 | +const dns = require('dns'); |
17 | 18 |
|
18 | | - ```js |
19 | | - const dns = require('dns'); |
| 19 | +dns.lookup('example.org', (err, address, family) => { |
| 20 | + console.log('address: %j family: IPv%s', address, family); |
| 21 | +}); |
| 22 | +// address: "93.184.216.34" family: IPv4 |
| 23 | +``` |
20 | 24 |
|
21 | | - dns.lookup('iana.org', (err, address, family) => { |
22 | | - console.log('address: %j family: IPv%s', address, family); |
23 | | - }); |
24 | | - // address: "192.0.43.8" family: IPv4 |
25 | | - ``` |
26 | | - |
27 | | -2. Functions that connect to an actual DNS server to perform name resolution, |
28 | | - and that _always_ use the network to perform DNS queries. This category |
29 | | - contains all functions in the `dns` module _except_ [`dns.lookup()`][]. |
30 | | - These functions do not use the same set of configuration files used by |
31 | | - [`dns.lookup()`][] (e.g. `/etc/hosts`). These functions should be used by |
32 | | - developers who do not want to use the underlying operating system's |
33 | | - facilities for name resolution, and instead want to _always_ perform DNS |
34 | | - queries. |
35 | | -
|
36 | | - Below is an example that resolves `'archive.org'` then reverse resolves the |
37 | | - IP addresses that are returned. |
38 | | -
|
39 | | - ```js |
40 | | - const dns = require('dns'); |
41 | | -
|
42 | | - dns.resolve4('archive.org', (err, addresses) => { |
43 | | - if (err) throw err; |
44 | | -
|
45 | | - console.log(`addresses: ${JSON.stringify(addresses)}`); |
46 | | -
|
47 | | - addresses.forEach((a) => { |
48 | | - dns.reverse(a, (err, hostnames) => { |
49 | | - if (err) { |
50 | | - throw err; |
51 | | - } |
52 | | - console.log(`reverse for ${a}: ${JSON.stringify(hostnames)}`); |
53 | | - }); |
54 | | - }); |
| 25 | +All other functions in the `dns` module connect to an actual DNS server to |
| 26 | +perform name resolution. They will always use the network to perform DNS |
| 27 | +queries. These functions do not use the same set of configuration files used by |
| 28 | +[`dns.lookup()`][] (e.g. `/etc/hosts`). These functions should be used by |
| 29 | +developers who do not want to use the underlying operating system's |
| 30 | +facilities for name resolution, and instead want to always perform DNS queries. |
| 31 | + |
| 32 | +```js |
| 33 | +const dns = require('dns'); |
| 34 | + |
| 35 | +dns.resolve4('archive.org', (err, addresses) => { |
| 36 | + if (err) throw err; |
| 37 | + |
| 38 | + console.log(`addresses: ${JSON.stringify(addresses)}`); |
| 39 | + |
| 40 | + addresses.forEach((a) => { |
| 41 | + dns.reverse(a, (err, hostnames) => { |
| 42 | + if (err) { |
| 43 | + throw err; |
| 44 | + } |
| 45 | + console.log(`reverse for ${a}: ${JSON.stringify(hostnames)}`); |
55 | 46 | }); |
56 | | - ``` |
| 47 | + }); |
| 48 | +}); |
| 49 | +``` |
57 | 50 |
|
58 | | -There are subtle consequences in choosing one over the other, please consult |
59 | | -the [Implementation considerations section][] for more information. |
| 51 | +See the [Implementation considerations section][] for more information. |
60 | 52 |
|
61 | 53 | ## Class: `dns.Resolver` |
62 | 54 | <!-- YAML |
|
0 commit comments