使用 Node.js 尝试获取以下网页的 html 内容时:
eternagame.wikia.com/wiki/EteRNA_Dictionary
我收到以下错误:
events.js:72
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND
at errnoException (dns.js:37:11)
at Object.onanswer [as oncomplete] (dns.js:124:16)
我确实已经在 stackoverflow 上查找了这个错误,并意识到这是因为 node.js 无法从 DNS 中找到服务器(我认为)。但是,我不确定为什么会这样,因为我的代码在 www.google.com
上运行良好。
这是我的代码(实际上是从一个非常相似的问题中复制和粘贴的,除了更改了主机):
var http = require("http");
var options = {
host: 'eternagame.wikia.com/wiki/EteRNA_Dictionary'
};
http.get(options, function (http_res) {
// initialize the container for our data
var data = "";
// this event fires many times, each time collecting another piece of the response
http_res.on("data", function (chunk) {
// append this chunk to our growing `data` var
data += chunk;
});
// this event fires *one* time, after all the `data` events/chunks have been gathered
http_res.on("end", function () {
// you can use res.send instead of console.log to output via express
console.log(data);
});
});
这是我复制和粘贴的来源:How to make web service calls in Expressjs?
我没有在 node.js 中使用任何模块。
谢谢阅读。
var http = require("http");
或 var https = require("https");
ENOTFOUND
是什么意思?
在 Node.js HTTP
模块的文档中:http://nodejs.org/api/http.html#http_http_request_options_callback
您可以调用 http.get('http://eternagame.wikia.com/wiki/EteRNA_Dictionary', callback)
,然后使用 url.parse()
解析 URL;或调用 http.get(options, callback)
,其中 options
是
{
host: 'eternagame.wikia.com',
port: 8080,
path: '/wiki/EteRNA_Dictionary'
}
更新
正如@EnchanterIO 的评论所述,port
字段也是一个单独的选项;并且协议 http://
不应包含在 host
字段中。如果需要 SSL,其他答案还建议使用 https
模块。
另一个常见的错误来源
Error: getaddrinfo ENOTFOUND
at errnoException (dns.js:37:11)
at Object.onanswer [as oncomplete] (dns.js:124:16)
在 options
中设置 host
属性时正在编写 协议(https、https、...)
// DON'T WRITE THE `http://`
var options = {
host: 'http://yoururl.com',
path: '/path/to/resource'
};
在 HTTP 请求的选项中,将其切换为
var options = { host: 'eternagame.wikia.com',
path: '/wiki/EteRNA_Dictionary' };
我认为这会解决你的问题。
我的问题是我的 OS X (Mavericks) DNS 服务需要重新启动。在 Catalina 和 Big Sur 上,可以通过以下方式清除 DNS 缓存:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
旧 macOS 版本 see here。
如果需要使用 https,则使用 https 库
https = require('https');
// options
var options = {
host: 'eternagame.wikia.com',
path: '/wiki/EteRNA_Dictionary'
}
// get
https.get(options, callback);
var http=require('http');
http.get('http://eternagame.wikia.com/wiki/EteRNA_Dictionary', function(res){
var str = '';
console.log('Response is '+res.statusCode);
res.on('data', function (chunk) {
str += chunk;
});
res.on('end', function () {
console.log(str);
});
});
我认为 http 在端口 80 上发出请求,即使我在选项对象中提到了完整的主机 url。当我在之前在端口 3000 上运行的端口 80 上运行具有 API 的服务器应用程序时,它可以工作。请注意,要在端口 80 上运行应用程序,您将需要 root 权限。
Error with the request: getaddrinfo EAI_AGAIN localhost:3000:80
这是一个完整的代码片段
var http=require('http');
var options = {
protocol:'http:',
host: 'localhost',
port:3000,
path: '/iso/country/Japan',
method:'GET'
};
var callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
console.log(str);
});
}
var request=http.request(options, callback);
request.on('error', function(err) {
// handle errors with the request itself
console.error('Error with the request:', err.message);
});
request.end();
host: https://server.com
的完整 uri,这里也提到了 stackoverflow.com/a/28385129/432903
我用这个修复了这个错误
$ npm info express --verbose
# Error message: npm info retry will retry, error on last attempt: Error: getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443
$ nslookup registry.npmjs.org
Server: 8.8.8.8
Address: 8.8.8.8#53
Non-authoritative answer:
registry.npmjs.org canonical name = a.sni.fastly.net.
a.sni.fastly.net canonical name = prod.a.sni.global.fastlylb.net.
Name: prod.a.sni.global.fastlylb.net
Address: 151.101.32.162
$ sudo vim /etc/hosts
# Add "151.101.32.162 registry.npmjs.org` to hosts file
$ npm info express --verbose
# Works now!
原始来源:https://github.com/npm/npm/issues/6686
请注意,如果您引用的域出现故障(例如,不再存在),也会出现此问题。
在我的情况下,错误是因为使用了不正确的主机值
var options = {
host: 'graph.facebook.com/v2.12/',
path: path
}
应该
var options = {
host: 'graph.facebook.com',
path: path
}
因此 .com 或 .net 等之后的任何内容都应移至路径参数值
我使用 request module 进行了尝试,并且能够非常轻松地打印出该页面的正文。不幸的是,以我拥有的技能,除此之外我无能为力。
从开发环境转到生产环境时出现此错误。我痴迷于将 https://
放在所有链接上。这不是必需的,因此它可能是某些人的解决方案。
我遇到了同样的错误,并在下面的链接中使用以获得帮助:
https://nodejs.org/api/http.html#http_http_request_options_callback
我的代码中没有:
req.end();
(NodeJs V: 5.4.0) 一旦添加到 req.end();
行上方,我就能够摆脱错误并且工作正常。
尝试使用服务器 IP 地址而不是主机名。这对我有用。希望它也对你有用。
我摆脱了 http 和额外的斜杠(/)。我刚刚使用了这个 'node-test.herokuapp.com' 并且它起作用了。
如果您仍然面临代理设置的结帐,对我来说,代理设置丢失并且无法发出请求,因为直接 http/https 被阻止。因此,我在发出请求时从我的组织配置了代理。
npm install https-proxy-agent
or
npm install http-proxy-agent
const httpsProxyAgent = require('https-proxy-agent');
const agent = new httpsProxyAgent("http://yourorganzation.proxy.url:8080");
const options = {
hostname: 'encrypted.google.com',
port: 443,
path: '/',
method: 'GET',
agent: agent
};
我通过从连接密码中删除不需要的字符解决了这个问题。例如,我有这些字符: <##% 并且它导致了问题(很可能哈希标签是问题的根本原因)。
我的问题是我们正在解析 url 并为 http.request() 生成 http_options;
我使用的是 request_url.host,它已经有带有域名的端口号,所以必须使用 request_url.hostname。
var request_url = new URL('http://example.org:4444/path');
var http_options = {};
http_options['hostname'] = request_url.hostname;//We were using request_url.host which includes port number
http_options['port'] = request_url.port;
http_options['path'] = request_url.pathname;
http_options['method'] = 'POST';
http_options['timeout'] = 3000;
http_options['rejectUnauthorized'] = false;
就我而言,问题是 URL 格式错误。我在 URL 中有双斜杠。
不定期副业成功案例分享
host
中包含实际主机名,所以没有http://
或https://
; 2. 不要在host
属性中包含路径,而是在path
属性中包含路径。options {...}
块时会出现奇怪的失败。