我将在Node.js中写出新查找的主机名的IP地址:
var net = require('net'); var sock = net.Socket(); sock.on('lookup', function(e) { console.log('DNS lookup'); console.log(address); } )
...
sock.connect(80, 'google.com');
https://nodejs.org/api/net.html#net_event_lookup
上面说 lookup
lookup
在解析主机名之后但在连接之前发出。不适用于UNIX套接字。
err <Error> | <null> The error object. See dns.lookup(). address <string> The IP address. family <string> | <null> The address type. See dns.lookup(). host <string> The hostname.
e 对象可能传递给我为 事件——这也不起作用。
e
在回调中获取所有参数,请参见下面的代码。
var net = require('net'); var sock = net.Socket(); const options = { host: "google.com", port: 80 }; sock.connect(options); sock.on('lookup', function(err, address, family, host) { console.log(address); console.log(family); console.log(host); console.log(err); });