Aphro Knight
Aphro Knight

Reputation: 1

My node.js http request worked for the first time and now it is showing some error

I am using node.js 'request' module for making http request. This code worked for the first time. But now it shows some error.

var request = require('request');
request('http://www.google.com', function (error, response, body) {
    if (!error && response.statusCode == 200) {
    console.log(body); 
} else {
    console.log(error);
}
})

Error:

{ [Error: getaddrinfo ENOTFOUND www.google.com]
 code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'www.google.com' }

I cannot understand why this is happening. My internet works fine and I can open google.com in my browser.

Upvotes: 0

Views: 97

Answers (1)

mscdex
mscdex

Reputation: 106746

It's a DNS issue. You could try explicitly using Google's DNS by doing:

require('dns').setServers(['8.8.8.8','8.8.4.4']);

Then perform your request.

Upvotes: 1

Related Questions