| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,9 +15,10 @@ For example, looking up `nodejs.org`. | |||
| 15 | 15 | ```js | |
| 16 | 16 | const dns = require('dns'); | |
| 17 | 17 | ||
| 18 | - dns.lookup('nodejs.org', (err, addresses, family) => { | ||
| 19 | - console.log('addresses:', addresses); | ||
| 18 | + dns.lookup('nodejs.org', (err, address, family) => { | ||
| 19 | + console.log('address: %j family: IPv%s', address, family); | ||
| 20 | 20 | }); | |
| 21 | + // address: "192.0.43.8" family: IPv4 | ||
| 21 | 22 | ``` | |
| 22 | 23 | ||
| 23 | 24 | 2) Functions that connect to an actual DNS server to perform name resolution, | |
@@ -84,15 +85,7 @@ Alternatively, `options` can be an object containing these properties: | |||
| 84 | 85 | * `all`: {Boolean} - When `true`, the callback returns all resolved addresses | |
| 85 | 86 | in an array, otherwise returns a single address. Defaults to `false`. | |
| 86 | 87 | ||
| 87 | - All properties are optional. An example usage of options is shown below. | ||
| 88 | - | ||
| 89 | - ```js | ||
| 90 | - { | ||
| 91 | - family: 4, | ||
| 92 | - hints: dns.ADDRCONFIG | dns.V4MAPPED, | ||
| 93 | - all: false | ||
| 94 | - } | ||
| 95 | - ``` | ||
| 88 | + All properties are optional. | ||
| 96 | 89 | ||
| 97 | 90 | The `callback` function has arguments `(err, address, family)`. `address` is a | |
| 98 | 91 | string representation of an IPv4 or IPv6 address. `family` is either the | |
@@ -115,6 +108,25 @@ important consequences on the behavior of any Node.js program. Please take some | |||
| 115 | 108 | time to consult the [Implementation considerations section][] before using | |
| 116 | 109 | `dns.lookup()`. | |
| 117 | 110 | ||
| 111 | + Example usage: | ||
| 112 | + | ||
| 113 | + ```js | ||
| 114 | + const dns = require('dns'); | ||
| 115 | + const options = { | ||
| 116 | + family: 6, | ||
| 117 | + hints: dns.ADDRCONFIG | dns.V4MAPPED, | ||
| 118 | + }; | ||
| 119 | + dns.lookup('example.com', options, (err, address, family) => | ||
| 120 | + console.log('address: %j family: IPv%s', address, family)); | ||
| 121 | + // address: "2606:2800:220:1:248:1893:25c8:1946" family: IPv6 | ||
| 122 | + | ||
| 123 | + // When options.all is true, the result will be an Array. | ||
| 124 | + options.all = true; | ||
| 125 | + dns.lookup('example.com', options, (err, addresses) => | ||
| 126 | + console.log('addresses: %j', addresses)); | ||
| 127 | + // addresses: [{"address":"2606:2800:220:1:248:1893:25c8:1946","family":6}] | ||
| 128 | + ``` | ||
| 129 | + | ||
| 118 | 130 | ### Supported getaddrinfo flags | |
| 119 | 131 | ||
| 120 | 132 | The following flags can be passed as hints to [`dns.lookup()`][]. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments