Summary of the DNS module in Node. js, node. jsdns

Source: Internet
Author: User
Tags get mx record mail exchange mx record subdomain name

Summary of the DNS module in Node. js, node. jsdns

1. DNS

In Node. js, the DNS module is provided for domain name search and resolution.

  • The DNS module provides three primary methods and a series of convenient methods.
  • Resolve Method: used to resolve a domain name to a group of DNS records.
  • Reverse Method: used to convert an IP address into a group of domain names.
  • Lookup method: used to convert a domain name to an IP address.
  • The other convenient methods in the DNS module are a convenient form of the resolve method.

2. Use the resolve method to resolve a domain name to a DNS record

'Dns. resolve (domain, [rrtype], callback (err, address ){...})'

The domain parameter is a string used to specify the domain name to be resolved, which can include a subdomain name.
The rrtypr parameter is a string used to specify the record type to be obtained. The record types that can be specified are as follows.

  • A. the parameter value is the default value. When the record type is A, the record maps an IPv4 address to A domain name.
  • AAAA. When the record type is AAAA, this record maps an IPv6 address to a domain name.
  • CNAME: when the record type is CNAME, it indicates that this record is an alias record for a domain name. For example, a Domain Name Record www.example.com may be an alias record for the example.com Domain Name Record.
  • MX, MX records point to the mail server in a domain that uses SMTP, for example, when you want to person@domain.com the mail address to send an email, the MX record in the domain.com domain stores the mail server address when the email is sent.
  • TXT and TXT records are the description records attached to the domain name.
  • SRV records are used to provide information for all available services in a specific domain.
  • PTR and PTR records are used for reverse address resolution. This record maps a domain name to an IPv4 address.
  • The NS (Name Server) record is a Domain Name Server record, used to specify the DNS Server to which the domain Name is resolved.

The callback function has two parameters. err is the error object triggered when domain name resolution fails. The addresses parameter is an array that stores all obtained DNS records.

3. Various convenient methods customized for the resolve Method

  • DNS. resolve4 (domain, callback), get IPv4 address
  • DNS. resolve6 (domain, callback), get IPv6 address
  • DNS. resolveMx (domain, callback), get MX record, mail exchange server record
  • DNS. resolveTxt (domain, callback), get TXT record, domain name additional description Record
  • DNS. resolveSrv (domain, callback), get SRV record, service record
  • DNS. resolveNs (domain, callback), get NS records, domain Name Server records
  • DNS. resolveCname (domain, callback), get the alias record

4. Use lookup to Query IP addresses

When the resolve4 or resolve6 method is used, the addresses parameter value array in the callback parameter value callback function stores all obtained IPv4 addresses or IPv6 addresses. Therefore, the DNS module provides a lookup method for obtaining the first discovered IPv4 address or IPv6 address.

'Dns. lookup (domain, [family], callback (err, addresses, family ){...})'

  • The domain parameter is a string used to specify the domain name to be resolved.
  • The family parameter is an integer used to specify the IP address type to be obtained. The parameter value can be 4 or 6. The default parameter value is null, indicating that IPv4 can be obtained, you can also obtain IPv6
  • The err parameter value of the callback function is the error object triggered when the address fails to be obtained. When the domain name does not exist or the query fails, the code attribute value of the error object is ENOENT.
  • The value of addresses is a string that indicates the obtained IP address.
  • If the family parameter is set to 4, it indicates an IPv4 address. If the family parameter is set to 6, it indicates an IPv6 address.

5. reverse IP Address Resolution using the reverse Method

In the DNS module, use the reverse method to reverse resolve an IP address to a group of domain names bound to this IP address.

'Dns. reverse (ip, callback (err, domains ){...})'

  • The ip parameter value is a string used to specify the ip address to be resolved.
  • The err of the callback function is the error object after the reverse resolution address fails.
  • The domains parameter value is an array that stores all obtained domain names.

6. Various error codes in the DNS Module

The err parameter value is the error object triggered when various parsing or reverse parsing operations are performed. You can determine the error according to the code attribute value of the error object, that is, the error code triggered.

  • ENODATA: the DNS server returns a query result without data.
  • EFORMERR: the DNS server uses incorrectly formatted query parameters when querying client requests.
  • ESERVFAIL: the DNS server failed to perform the query operation.
  • ENOTFOUND: No domain name found
  • Enoibd: the DNS server cannot perform the query operation requested by the client.
  • EREFUSED: the DNS server rejects the query operation.
  • EBADQUERY: incorrectly formatted DNS query
  • EBADNAME: Incorrect domain name format
  • EBADFAMILY: Unsupported IP Address type
  • EBADRESP: the DNS response format is incorrect.
  • ECONNREFUSED: cannot establish a connection with the DNS server
  • ETIMEOUT: the connection to the DNS server has timed out.
  • EEOF: reached the bottom of the file
  • EFILE: failed to read the file.
  • ENOMEM: insufficient memory space
  • EDESTRUCTION: the channel has been destroyed.
  • EBADSTR: the string format is incorrect.
  • EBADFLAGS: Specifies an error flag.
  • ENONAME: the specified host name is not in numerical format.
  • EBADHINTS: the specified prompt flag is invalid.
  • ENOTINITIALIZED: the initialization of the c-ares class library has not been completed.
  • ELOADIPHLPAPI: an error is triggered when iphlpapi. dll is loaded.
  • EADDREGETNETWORKPARAMS: No GetNetworkParams function found
  • ECANCELLED: DNS query operation canceled

7. basic use of the DNS Module

Const dns = require ('dns'); let url = 'www .qq.com '; dns. resolve (url, 'A', (err, addresses) => {console. log (addresses); // IPv4 address ['2017. 7.30.123 ']}); dns. resolve (url, 'aaa', (err, addresses) => {console. log (addresses); // IPv6 Address ['240e: e1: 8100: 28: 2: 16']}); dns. resolveMx ('qq. com ', (err, addresses) => {console. log (addresses); // mail exchange server record // [{exchange: 'mx2 .qq.com ', priority: 20}, // {exchange: 'mx1 .qq.com', priority: 30}, // {exchange: 'mx3 .qq.com ', priority: 10}]}); dns. resolveTxt ('qq. com ', (err, addresses) => {console. log (addresses); // description Record appended to the domain name // [['v = spf1 include: spf.mail.qq.com-all']}); dns. resolveSrv ('www .baidu.com ', (err, addresses) => {console. log (addresses); // service record // []}); dns. resolveNs ('www .github.com ', (err, addresses) => {console. log (addresses); // Domain Name Server record // ['ns done', // 'ns -5379awsdns-01.net ', // 'ns.p16.dynect.net', // 'ns.p16.dynect.net ', // 'ns3 .p16.dynect.net ', // 'ns4 .p16.dynect.net', // 'ns -1283.awsdns-32.org ', // 'ns-1707. awsdns-21.co.uk ']}); dns. resolveCname ('www .163.com ', (err, addresses) => {console. log (addresses); // obtain the alias record // ['www .163.com.lxdns.com ']}); dns. lookup ('Google. com ', 4, (err, address, family) => {// query the IP address // address, the queried address // family, IPv4 or IPv6 console. log (address); // 172.217.27.142 console. log (family); // 4}); dns. lookup ('Google. com ', 6, (err, address, family) => {console. log (address); // 2404: 6800: 4008: 803: 200e console. log (family); // 6}); dns. reverse ('2017. 188.200.67 ', (err, domain) =>{// reverse resolution IP address console. log (domain); // ['Media -router-fp1.prod.media.vip.tp2.yahoo.com ']});

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.