I need to lookup a given host to its corresponding IP in Node.js. There seems to be two native methods of doing this:
> dns.resolve('google.com', (error, addresses) => { console.error(error); console.log(addresses); });
QueryReqWrap {
bindingName: 'queryA',
callback: { [Function: asyncCallback] immediately: true },
hostname: 'google.com',
oncomplete: [Function: onresolve],
domain:
Domain {
domain: null,
_events: { error: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
members: [] } }
> null
[ '216.58.194.174' ]
And:
> dns.lookup('google.com', (error, address, family) => { console.error(error); console.log(address); console.log(family); });
GetAddrInfoReqWrap {
callback: { [Function: asyncCallback] immediately: true },
family: 0,
hostname: 'google.com',
oncomplete: [Function: onlookup],
domain:
Domain {
domain: null,
_events: { error: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
members: [] } }
> null
216.58.194.174
4
Both return the same IPv4 address. What is the difference between dns.lookup()
and dns.resolve()
? Also, which is more performant for lots of requests per second?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…