The Nodejs DNS module contains methods for DNS queries and operations, the basic usage of the module, and the implementation of a DNS query gadget.
1. Get DNS server address
Using the getServers
method, the method returns an array of IP addresses, as follows:
Const DNS = require (' DNS ');
Const Servers = dns.getservers ();
Console.log (servers);
The result returned is:
[' 114.114.114.114 ', ' 8.8.8.8 ',
' Fec0:0:0:ffff::1 ', ' 114.114.114.114 ',
' 8.8.8.8 ', ' 114.114.114.114 ',
' 8.8.8.8 ']
2. Use the system characteristic domain name resolution obtains the IP address
Using dns.lookup(hostname[, options], callback)
the method, the options parameter includes the following properties:
- Family: Address protocol family, must be an integer of 4 or 6
- Hints: Set the GETADDRINFO flag, DNS. Addrconfig or DNS. v4mapped (IPv4 map to IPv6)
- All:false (Default), Boolean value, if set to True, returns an IP array, otherwise returns a single IP address
The callback callback function has three parameters (err,address,family), and if the all property of the options is set to True, only the (err,addresses) parameter and addresses is an array with the array element {address , family} object. Use the following example:
Dns.lookup (' www.baidu.com ', (err,address,family) =>{
if (err) throw err;
Console.log (' Baidu website IP address is: ' +address+ ' Address protocol family is: IPV ' +family);
});
The results are as follows:
E:\developmentdocument\nodejsdemo>node Dns-example.js
Baidu site's IP address is: 14.215.177.37 Address protocol family is: IPV4
When all of the options is set to true, the result is as follows:
Dns.lookup (' www.baidu.com ', {family:4,all:!0,hints:dns. Addrconfig|dns. v4mapped}, (err,addresses) =>{
if (err) throw err;
Addresses.foreach ((Ele,idx,arr) =>{
console.log (' Baidu website's IP address ' + (idx+1) + ' is: ' +ele.address ';
});
};
The results are as follows:
E:\developmentdocument\nodejsdemo>node Dns-example.js
The IP address of Baidu website 1 is: 14.215.177.38
The IP address of Baidu website 2 is: 14.215.177.37
3. Get host name based on IP and port
Using the dns.lookupService(address, port, callback)
method, this method relies on the getnameinfo underlying function.
The callback function has three parameters (err, hostname, service), Service is protocol, HTTP or HTTPS, used as follows:
Dns.lookupservice (' 127.0.0.1 ', err,hostname,service) =>{
if (err) console.log (err);
Console.log (' The IP corresponding host is: ' +hostname+ ' protocol is: ' +service ');
};
The results are as follows:
E:\developmentdocument\nodejsdemo>node Dns-example.js
The IP-corresponding host is: Www.test.zmx.com protocol: HTTP
4. Use network domain name resolution to obtain IP address
Using dns.resolve(hostname[, rrtype], callback)
the method, Rrtype has the following choices:
- ' A ': Ipv4,default
- ' AAAA ': IPV6
- ' MX '-Mail exchange records mail exchange records
- ' TXT '-Text records domain name configuration description
- ' SRV '-service provided by the SRV records server
- ' PTR '-PTR records
- ' NS '-name server records domain name servers
- ' CNAME '-canonical name records alias record
- ' SOA '-start of authority record start authority
- ' Naptr '-name Authority pointer record
The callback function has (err, addresses) two parameters, addresses is an array, the specific members need to see the specific rrtype, using the following:
Get IPV4
dns.resolve (' www.qq.com ', ' A ', (err,address) =>{
if (err) throw err;
Console.log (address);//The result is [' 14.17.32.211 ', ' 14.17.42.40 ', ' 59.37.96.63 ']
});
Get IPV6
dns.resolve (' www.qq.com ', ' AAAA ', (err,address) =>{
if (err) throw err;
Console.log (address);//The result is [' 240e:ff:f040:28::a ']
});
Get SOA information
dns.resolve (' www.qq.com ', ' SOA ', (err,address) =>{
if (err) throw err;
Console.log (address);
The result is
{nsname: ' ns-tel1.qq.com ',
hostmaster: ' webmaster.qq.com ',
serial:1380440321,
refresh: retry:600,
expire:86400,
minttl:300}
});
Gets the alias CNAME
dns.resolve (' www.baidu.com ', ' CNAME ', (err,address) =>{
if (err) throw err;
Console.log (address);//The result is [' www.a.shifen.com ']
});
Resovle also has many shortcut methods, such as: Resolve4,resolve6,resolvecname ... Wait a minute
5. reverse Domain Name resolution
Using the dns.reverse(ip, callback)
method, callback has two parameters (err, hostnames), and hostnames is an array of domain names, using the following:
Dns.reverse (' 114.114.114.114 ', (err,hostnames) =>{
if (err) throw err;
Console.log (hostnames);//The result is [' public1.114dns.com ']
});
After learning the above knowledge, you can do a small tool for DNS queries, as follows:
The first step is to write an HTML static page, as follows:
The operation effect is as follows:
This gadget is finished.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.