Home > Web Front-end > JS Tutorial > body text

Detailed explanation of domain name DNS in nodeJS

零下一度
Release: 2017-06-26 13:33:22
Original
1704 people have browsed it

Previous words

This article will introduce the domain name resolution module DNS in detail

Working principle

Open the browser and enter the URL in the address bar above At that moment, after pressing Enter, a lot of things happened. First of all, the computer only understands 0 and 1, which means that the computer does not understand human alphabetical addresses. It only understands IP addresses. If it is IPv4, it is four groups of 8-bit binary numbers. For human convenience, there needs to be a service that translates URLs into IP addresses, which is DNS

The entire DNS acquisition process is cached layer by layer

## 1 , The browser searches its own DNS cache

The browser DNS cache time has nothing to do with the TTL value returned by the DNS server.

After the browser obtains the actual IP address of the website domain name, it will cache its IP to reduce the loss of network requests. Each browser has a fixed DNS cache time, of which Chrome's expiration time is 1 minute. During this period, DNS will not be re-requested

It is more convenient for the Chrome browser to check its own DNS cache time. Enter

chrome://net-internals/#dns
Copy after login
in the address bar 2. Search the operating system’s own DNS cache

3. Read the local HOST file, Windows The download path is generally

c:\Windows\System32\drivers\etc\hosts
Copy after login
4. Initiate a DNS system call to the broadband operator ISP, and the ISP server checks its own cache

5. If it is not found yet, the ISP server will initiate an iterative DNS resolution request on behalf of the local computer

6. If it is still unsuccessful, the resolution fails

Local Resolution

The dns module contains two types of functions, one of which is a function that uses underlying operating system tools to perform domain name resolution and does not require network communication. There is only one such function: dns.lookup()

[dns.lookup(hostname[, options], callback)】

This method resolves the domain name (such as 'cnblogs.com') The first record found is A (IPV4) or AAAA (IPV6). Parameter options can be an object or an integer. If no options are provided, both IP v4 and v6 addresses are acceptable. If options is an integer, it must be 4 or 6

The options parameter contains the following attributes

family:地址协议族,必须为4或6的整数
hints:设置getaddrinfo的标志,dns.ADDRCONFIG 或者 dns.V4MAPPED(ipv4映射成ipv6)
all:false(默认),布尔值,如设置为true,则返回IP数组,否则返回单个IP地址
Copy after login
{
  family: 4,
  hints: dns.ADDRCONFIG | dns.V4MAPPED
}
Copy after login
The callback function contains parameters (err, address , family). The address parameter represents an IP v4 or v6 address. The family parameter is 4 or 6, indicating the address family (not necessarily the value passed into lookup before). When an error occurs, the parameter err is the Error object, and err.code is the error code

[Note] err.code is equal to 'ENOENT', which may be because the domain name does not exist, or other reasons, such as no available files. Descriptor

var dns = require('dns');
dns.lookup('www.cnblogs.com', function(err, address, family){
    console.log(err);//nullconsole.log(address);//218.11.2.249console.log(family);//4});
Copy after login
The same domain name may correspond to multiple different IPs. You can get it by setting options = {all: true}

var dns = require('dns');
dns.lookup('www.qq.com',{all:true}, function(err, address, family){
    console.log(err);//null/*[ { address: '125.39.240.113', family: 4 },
  { address: '61.135.157.156', family: 4 } ] */console.log(address);
    console.log(family);//undefined});
Copy after login
【dns.lookupService(address, port, callback)】

Corresponding to lookup, lookupService( ) method performs reverse resolution from ip address and port to domain name

The parameters of the callback function of this method are (err, hostname, service). hostname and service are both strings (such as 'localhost' and 'http'). When an error occurs, the parameter err is the Error object, and err.code is the error code

var dns = require('dns');
dns.lookupService('127.0.0.1',80,function(err, hostname, service){
    console.log(err);//nullconsole.log(hostname);//baiconsole.log(service);//http});
Copy after login

Network Analysis

Except for dns.lookup() All functions in the dns module need to connect to the actual DNS server for domain name resolution, and always use the network to perform DNS queries

[dns.resolve(hostname[, rrtype], callback)]

This method parses a domain name (such as 'cnblogs.com') into an array of rrtype specified record types

The valid rrtypes value is:

'A' (IPV4 地址, 默认)'AAAA' (IPV6 地址)'MX' (邮件交换记录)'TXT' (text 记录)'SRV' (SRV 记录)'PTR' (用来反向 IP 查找)'NS' (域名服务器 记录)'CNAME' (别名 记录)'SOA' (授权记录的初始值)
Copy after login
The callback parameter is

(err, addresses). The type of each item in addresses depends on the record type. When an error occurs, the parameter err is the Error object, and err.code is the error code

var dns = require('dns');//IPV4dns.resolve('www.qq.com',function(err,address){
    console.log(address);//[ '125.39.240.113', '61.135.157.156' ]});//IPV6dns.resolve('www.qq.com','AAAA',function(err,address){
    console.log(address);//[ '240e:e1:8100:28::2:16' ]});//别名dns.resolve('www.qq.com','CNAME',function(err,address){
    console.log(address);//undefined});
Copy after login
[dns.resolve4 (hostname, callback)】

 Similar to dns.resolve(), only IPv4 (A record) can be queried

var dns = require('dns');
dns.resolve4('www.qq.com',function(err,address){
    console.log(address);//[ '125.39.240.113', '61.135.157.156' ]});
Copy after login
【dns.reverse(ip, callback) 】

 This method is used to reversely resolve the IP address and return the domain name array pointing to the IP address. Callback function parameters (err, hostnames). When an error occurs, the parameter err is an Error object, and err.code is the error code

var dns = require('dns');
dns.reverse('114.114.114.114',function(err,hostnames){
    console.log(hostnames);//'public1.114dns.com'});
Copy after login

The above is the detailed content of Detailed explanation of domain name DNS in nodeJS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template