简体   繁体   中英

node.js request module getaddrinfo error

I am trying to hit http://api.openweathermap.org using my assigned apikey ,it is working fine with browser but when i am trying using request module it is giving me error:

code is

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);
  }
}); 

and the error is;

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 }

Can you try using different DNS servers? You can do it in Windows/Mac OS/ Linux or explicitly in 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 is one of Google's DNS servers. You can try with whatever suits you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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