强制net / http使用c-ares而不是getaddrinfo()
正如文件所说:
除了在线程池中使用getaddrinfo(3)的dns.lookup以外,dns模块中的所有方法都使用C-Ares。
但是, net
和http
模块总是使用dns.lookup
。 有没有办法改变这个? getaddrinfo
是同步的,池将只允许4个并发请求。
看了这个之后,似乎唯一的办法是手动执行DNS请求。 我使用这样的东西:
var dns = require('dns'), http = require('http'); function httpRequest(options, body, done) { var hostname = options.hostname || options.host; if (typeof(body) === 'function') { done = body; body = null; } // async resolve with C-ares dns.resolve(hostname, function (err, addresses) { if (err) return done(err); // Pass the host in the headers so the remote webserver // can use the correct vhost. var headers = options.headers || {}; headers.host = hostname; options.headers = headers; // pass the resolved address so http doesn't feel the need to // call dns.lookup in a thread pool options.hostname = addresses[0]; options.host = undefined; var req = http.request(options, done); req.on('error', done); if (body) req.write(body); req.end(); }); }