node.js请求模块getaddrinfo错误

时间:2018-08-20 06:54:51

标签: node.js api request

我正在尝试使用分配的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 }

1 个答案:

答案 0 :(得分:0)

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

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