首页 > 代码库 > nodeJS之域名DNS

nodeJS之域名DNS

前面的话

  本文将详细介绍域名解析模块DNS

 

本地解析

  dns模块包含两个类型的函数,其中一种是使用底层操作系统工具进行域名解析的函数,并不须要进行网络通信。这类函数只有一个:dns.lookup()

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

  该方法将域名(比如‘cnblogs.com‘)解析为第一条找到的记录 A (IPV4)或 AAAA(IPV6)。参数 options可以是一个对象或整数。如果没有提供 options,IP v4 和 v6 地址都可以。如果 options 是整数,则必须是 4 或 6

  options参数包含以下属性

family:地址协议族,必须为4或6的整数hints:设置getaddrinfo的标志,dns.ADDRCONFIG 或者 dns.V4MAPPED(ipv4映射成ipv6)all:false(默认),布尔值,如设置为true,则返回IP数组,否则返回单个IP地址
{  family: 4,  hints: dns.ADDRCONFIG | dns.V4MAPPED}

  回调函数包含参数 (err, address, family)。 address参数表示 IP v4 或 v6 地址。family 参数是4 或 6,表示 address 家族(不一定是之前传入 lookup 的值)。出错时,参数 err 是 Error 对象,err.code是错误代码

  [注意]err.code等于‘ENOENT‘,可能是因为域名不存在,还有可能是其他原因,如没有可用文件描述符

var dns = require(‘dns‘);dns.lookup(‘www.cnblogs.com‘, function(err, address, family){    console.log(err);//null    console.log(address);//218.11.2.249    console.log(family);//4});

  同一个域名,可能对应多个不同的ip。可以通过设置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});

【dns.lookupService(address, port, callback)】

  与lookup相对应,lookupService()方法进行从ip地址和端口到域名的反向解析

  该方法的回调函数的参数是 (err, hostname, service)。 hostname 和 service 都是字符串 (比如 ‘localhost‘ 和 ‘http‘)。出错时,参数err 是 Error 对象,err.code是错误代码

var dns = require(‘dns‘);dns.lookupService(‘127.0.0.1‘,80,function(err, hostname, service){    console.log(err);//null    console.log(hostname);//bai    console.log(service);//http});

 

网络解析

  除dns.lookup()以外的所有dns模块中的函数,都需要连接到实际DNS服务器进行域名解析的函数,并且始终使用网络执行DNS查询

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

  该方法将一个域名(如 ‘cnblogs.com‘)解析为一个 rrtype 指定记录类型的数组

  有效的 rrtypes 值为:

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

  回调参数为 (err, addresses). 其中 addresses 中每一项的类型都取决于记录类型。出错时,参数err 是 Error 对象,err.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});

【dns.resolve4(hostname, callback)】

  和 dns.resolve() 类似,仅能查询 IPv4 (A 记录)

var dns = require(‘dns‘);dns.resolve4(‘www.qq.com‘,function(err,address){    console.log(address);//[ ‘125.39.240.113‘, ‘61.135.157.156‘ ]});

【dns.reverse(ip, callback)】

  该方法用于反向解析 IP 地址,返回指向该 IP 地址的域名数组。回调函数参数 (err, hostnames)。出错时,参数err 是 Error 对象,err.code是错误代码

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

 

<script type="text/javascript" src="http://files.cnblogs.com/files/xiaohuochai/contextMenu.js"></script><script type="text/javascript" src="http://files.cnblogs.com/files/xiaohuochai/catalog.js"></script>

nodeJS之域名DNS