Node.js 强制请求使用/etc/hosts

Node.js 强制请求使用/etc/hosts,node.js,proxy,request,hosts,Node.js,Proxy,Request,Hosts,我基本上有一个内部url,它映射到/etc/hosts文件中的ip地址。当我在url上执行ping时,将返回正确的内部ip地址。依赖请求节点模块时会出现问题: /etc/主机: 123.123.123.123 fakeurl.com app.js: 403错误: var request = require('request'); request('http://fakeurl.com/', function (error, response, body) { console.log(

我基本上有一个内部url,它映射到/etc/hosts文件中的ip地址。当我在url上执行ping时,将返回正确的内部ip地址。依赖
请求
节点模块时会出现问题:

/etc/主机:

123.123.123.123 fakeurl.com
app.js:

403错误:

var request = require('request');
request('http://fakeurl.com/', function (error, response, body) {
     console.log('error:', error); // Print the error if one occurred
     console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
     console.log('body:', body); // Print the HTML for the page.
});
工程200代码:

var request = require('request');
request('http://123.123.123.123/', function (error, response, body) {
     console.log('error:', error); // Print the error if one occurred
     console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
     console.log('body:', body); // Print the HTML for the page.
});

有没有办法在节点应用程序中强制dns映射?

节点()使用的默认dns解析方法使用系统解析程序,它几乎总是考虑/etc/hosts

这里的差异与DNS解析本身无关,但很可能与HTTP
Host
字段使用的值有关。在第一个请求中,
Host:fakeurl.com
将被发送到位于123.123.123.123的HTTP服务器,而在第二个请求中,
Host:123.123.123
将被发送到位于123.123.123.123的HTTP服务器。服务器可能会根据这两个请求的配置对其进行不同的解释

因此,如果要将IP地址用作HTTP
Host
头字段值,则需要手动解析该地址。例如:

require('dns').lookup('fakeurl.com', (err, ip) => {
  if (err) throw err;
  request(`http://${ip}/`, (error, response, body) => {
    // ...
  });
});

您可以改用http模块试试吗?http模块应该根据/etc/hosts进行解析。我希望它能这么简单。我被绑定到请求模块,因为它实际上是由另一个节点模块执行的。我只是缩小了它为什么会发生的问题,并希望找到一种绕过它的方法。我面临着同样的问题。我不知道Node.js为什么一直让请求忽略主机文件。兄弟,你找到解决方案了吗?我实际上不得不向导致问题的git回购发出请求。幸运的是,店主接受了我的更改。但愿我能给你一个更好的答案。