繁体   English   中英

node.js请求模块getaddrinfo错误

[英]node.js request module getaddrinfo error

我正在尝试使用分配的apikey来访问http://api.openweathermap.org ,它在浏览器中正常工作,但是当我尝试使用请求模块时,它给了我错误:

代码是

apiKey = 'cb1d43c417fce6fa41e4b2251d76fbf2';
city = 'mysore';
url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}`;
var request = require('request');
request(url, function(err, response, body) {
  if (err) {
    console.log('error:', err);
  } else {
    console.log('body:', body);
  }
}); 

错误是;

C:\Users\manu.varghese\node-weather>node index.js
error: { Error: getaddrinfo EAI_AGAIN api.openweathermap.org:80
at Object._errnoException (util.js:992:11)
at errnoException (dns.js:55:15)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26)
code: 'EAI_AGAIN',
errno: 'EAI_AGAIN',
syscall: 'getaddrinfo',
hostname: 'api.openweathermap.org',
host: 'api.openweathermap.org',
port: 80 }

您可以尝试使用其他DNS服务器吗? 您可以在Windows / Mac OS / Linux中或在Node中显式地执行此操作: let dns = require('dns'); dns.setServers(['8.8.8.8']); let dns = require('dns'); dns.setServers(['8.8.8.8']);

8.8.8.8是Google的DNS服务器之一。 您可以尝试任何适合您的方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM