使用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)
Run Code Online (Sandbox Code Playgroud)
我确实已经在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);
});
});
Run Code Online (Sandbox Code Playgroud)
以下是我复制和粘贴的来源:如何在Expressjs中进行Web服务调用?
我没有使用node.js的任何模块.
谢谢阅读,
维尼特
yux*_*ang 263
在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 url.parse(); 或致电http.get(options, callback),这里options是
{
host: 'eternagame.wikia.com',
port: 8080,
path: '/wiki/EteRNA_Dictionary'
}
Run Code Online (Sandbox Code Playgroud)
更新
正如@EnchanterIO的评论中所述,该port字段也是一个单独的选项; 该协议http://不应包括在该host领域.https如果需要SSL,其他答案还建议使用模块.
Jor*_*ran 232
另一个常见的错误来源
Error: getaddrinfo ENOTFOUND
at errnoException (dns.js:37:11)
at Object.onanswer [as oncomplete] (dns.js:124:16)
Run Code Online (Sandbox Code Playgroud)
在设置属性时写入协议(https,https,...)hostoptions
// DON'T WRITE THE `http://`
var options = {
host: 'http://yoururl.com',
path: '/path/to/resource'
};
Run Code Online (Sandbox Code Playgroud)
Rus*_*ear 17
在HTTP请求的选项中,将其切换为
var options = { host: 'eternagame.wikia.com',
path: '/wiki/EteRNA_Dictionary' };
Run Code Online (Sandbox Code Playgroud)
我认为这将解决你的问题.
sac*_*hin 12
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);
});
});
Run Code Online (Sandbox Code Playgroud)
And*_*rew 11
如果您需要使用https,请使用https库
https = require('https');
// options
var options = {
host: 'eternagame.wikia.com',
path: '/wiki/EteRNA_Dictionary'
}
// get
https.get(options, callback);
Run Code Online (Sandbox Code Playgroud)
我用这个修复了这个错误
$ 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!
Run Code Online (Sandbox Code Playgroud)
原始来源: https: //github.com/npm/npm/issues/6686
我认为http在端口80上发出了请求,即使我在options对象中提到了完整的主机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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
417830 次 |
| 最近记录: |