The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / doc / api / dns.markdown @ 8886c6bf
History | View | Annotate | Download (5.87 KB)
1 |
# DNS |
---|---|
2 |
|
3 |
Stability: 3 - Stable |
4 |
|
5 |
Use `require('dns')` to access this module. All methods in the dns module |
6 |
use C-Ares except for `dns.lookup` which uses `getaddrinfo(3)` in a thread |
7 |
pool. C-Ares is much faster than `getaddrinfo` but the system resolver is |
8 |
more constant with how other programs operate. When a user does |
9 |
`net.connect(80, 'google.com')` or `http.get({ host: 'google.com' })` the |
10 |
`dns.lookup` method is used. Users who need to do a large number of lookups |
11 |
quickly should use the methods that go through C-Ares. |
12 |
|
13 |
Here is an example which resolves `'www.google.com'` then reverse |
14 |
resolves the IP addresses which are returned. |
15 |
|
16 |
var dns = require('dns'); |
17 |
|
18 |
dns.resolve4('www.google.com', function (err, addresses) { |
19 |
if (err) throw err; |
20 |
|
21 |
console.log('addresses: ' + JSON.stringify(addresses)); |
22 |
|
23 |
addresses.forEach(function (a) { |
24 |
dns.reverse(a, function (err, domains) { |
25 |
if (err) { |
26 |
throw err; |
27 |
} |
28 |
|
29 |
console.log('reverse for ' + a + ': ' + JSON.stringify(domains)); |
30 |
}); |
31 |
}); |
32 |
}); |
33 |
|
34 |
## dns.lookup(domain, [family], callback) |
35 |
|
36 |
Resolves a domain (e.g. `'google.com'`) into the first found A (IPv4) or |
37 |
AAAA (IPv6) record. |
38 |
The `family` can be the integer `4` or `6`. Defaults to `null` that indicates |
39 |
both Ip v4 and v6 address family. |
40 |
|
41 |
The callback has arguments `(err, address, family)`. The `address` argument |
42 |
is a string representation of a IP v4 or v6 address. The `family` argument |
43 |
is either the integer 4 or 6 and denotes the family of `address` (not |
44 |
necessarily the value initially passed to `lookup`). |
45 |
|
46 |
On error, `err` is an `Error` object, where `err.code` is the error code. |
47 |
Keep in mind that `err.code` will be set to `'ENOENT'` not only when |
48 |
the domain does not exist but also when the lookup fails in other ways |
49 |
such as no available file descriptors. |
50 |
|
51 |
|
52 |
## dns.resolve(domain, [rrtype], callback) |
53 |
|
54 |
Resolves a domain (e.g. `'google.com'`) into an array of the record types |
55 |
specified by rrtype. Valid rrtypes are `'A'` (IPV4 addresses, default), |
56 |
`'AAAA'` (IPV6 addresses), `'MX'` (mail exchange records), `'TXT'` (text |
57 |
records), `'SRV'` (SRV records), `'PTR'` (used for reverse IP lookups), |
58 |
`'NS'` (name server records) and `'CNAME'` (canonical name records). |
59 |
|
60 |
The callback has arguments `(err, addresses)`. The type of each item |
61 |
in `addresses` is determined by the record type, and described in the |
62 |
documentation for the corresponding lookup methods below. |
63 |
|
64 |
On error, `err` is an `Error` object, where `err.code` is |
65 |
one of the error codes listed below. |
66 |
|
67 |
|
68 |
## dns.resolve4(domain, callback) |
69 |
|
70 |
The same as `dns.resolve()`, but only for IPv4 queries (`A` records). |
71 |
`addresses` is an array of IPv4 addresses (e.g. |
72 |
`['74.125.79.104', '74.125.79.105', '74.125.79.106']`). |
73 |
|
74 |
## dns.resolve6(domain, callback) |
75 |
|
76 |
The same as `dns.resolve4()` except for IPv6 queries (an `AAAA` query). |
77 |
|
78 |
|
79 |
## dns.resolveMx(domain, callback) |
80 |
|
81 |
The same as `dns.resolve()`, but only for mail exchange queries (`MX` records). |
82 |
|
83 |
`addresses` is an array of MX records, each with a priority and an exchange |
84 |
attribute (e.g. `[{'priority': 10, 'exchange': 'mx.example.com'},...]`). |
85 |
|
86 |
## dns.resolveTxt(domain, callback) |
87 |
|
88 |
The same as `dns.resolve()`, but only for text queries (`TXT` records). |
89 |
`addresses` is an array of the text records available for `domain` (e.g., |
90 |
`['v=spf1 ip4:0.0.0.0 ~all']`). |
91 |
|
92 |
## dns.resolveSrv(domain, callback) |
93 |
|
94 |
The same as `dns.resolve()`, but only for service records (`SRV` records). |
95 |
`addresses` is an array of the SRV records available for `domain`. Properties |
96 |
of SRV records are priority, weight, port, and name (e.g., |
97 |
`[{'priority': 10, {'weight': 5, 'port': 21223, 'name': 'service.example.com'}, ...]`). |
98 |
|
99 |
## dns.resolveNs(domain, callback) |
100 |
|
101 |
The same as `dns.resolve()`, but only for name server records (`NS` records). |
102 |
`addresses` is an array of the name server records available for `domain` |
103 |
(e.g., `['ns1.example.com', 'ns2.example.com']`). |
104 |
|
105 |
## dns.resolveCname(domain, callback) |
106 |
|
107 |
The same as `dns.resolve()`, but only for canonical name records (`CNAME` |
108 |
records). `addresses` is an array of the canonical name records available for |
109 |
`domain` (e.g., `['bar.example.com']`). |
110 |
|
111 |
## dns.reverse(ip, callback) |
112 |
|
113 |
Reverse resolves an ip address to an array of domain names. |
114 |
|
115 |
The callback has arguments `(err, domains)`. |
116 |
|
117 |
On error, `err` is an `Error` object, where `err.code` is |
118 |
one of the error codes listed below. |
119 |
|
120 |
## dns.getServers() |
121 |
|
122 |
Returns an array of IP addresses as strings that are currently being used for |
123 |
resolution |
124 |
|
125 |
## dns.setServers(servers) |
126 |
|
127 |
Given an array of IP addresses as strings, set them as the servers to use for |
128 |
resolving |
129 |
|
130 |
If you specify a port with the address it will be stripped, as the underlying |
131 |
library doesn't support that. |
132 |
|
133 |
This will throw if you pass invalid input. |
134 |
|
135 |
## Error codes |
136 |
|
137 |
Each DNS query can return one of the following error codes: |
138 |
|
139 |
- `dns.NODATA`: DNS server returned answer with no data. |
140 |
- `dns.FORMERR`: DNS server claims query was misformatted. |
141 |
- `dns.SERVFAIL`: DNS server returned general failure. |
142 |
- `dns.NOTFOUND`: Domain name not found. |
143 |
- `dns.NOTIMP`: DNS server does not implement requested operation. |
144 |
- `dns.REFUSED`: DNS server refused query. |
145 |
- `dns.BADQUERY`: Misformatted DNS query. |
146 |
- `dns.BADNAME`: Misformatted domain name. |
147 |
- `dns.BADFAMILY`: Unsupported address family. |
148 |
- `dns.BADRESP`: Misformatted DNS reply. |
149 |
- `dns.CONNREFUSED`: Could not contact DNS servers. |
150 |
- `dns.TIMEOUT`: Timeout while contacting DNS servers. |
151 |
- `dns.EOF`: End of file. |
152 |
- `dns.FILE`: Error reading file. |
153 |
- `dns.NOMEM`: Out of memory. |
154 |
- `dns.DESTRUCTION`: Channel is being destroyed. |
155 |
- `dns.BADSTR`: Misformatted string. |
156 |
- `dns.BADFLAGS`: Illegal flags specified. |
157 |
- `dns.NONAME`: Given hostname is not numeric. |
158 |
- `dns.BADHINTS`: Illegal hints flags specified. |
159 |
- `dns.NOTINITIALIZED`: c-ares library initialization not yet performed. |
160 |
- `dns.LOADIPHLPAPI`: Error loading iphlpapi.dll. |
161 |
- `dns.ADDRGETNETWORKPARAMS`: Could not find GetNetworkParams function. |
162 |
- `dns.CANCELLED`: DNS query cancelled. |
163 |
|