nodeJs: dns.lookup error

nodeJs: dns.lookup error
0

#1

This is my code

let dns = require('dns');
dns.lookup("https://forum.freecodecamp.org", (err, add, fam) => {
    console.log("err:  " + err, "add: " + add, "fam: " + fam);
});

Why I am getting this in the console

err: Error: getaddrinfo EAI_AGAIN https://forum.freecodecamp.org add: undefined fam: undefined


#2

EAI_AGAIN is a DNS lookup timeout error… The Stack Overflow link below has an explanation and references to other links that may help:

Error: getaddrinfo EAI_AGAIN

You get add and fam as undefined because, since the lookup fails, they’re not set.

Also, a host name is the server name, not the URL, so you’d want to use

forum.freecodecamp.org

not

https://forum.freecodecamp.org

in the lookup

dns.lookup("forum.freecodecamp.org", (err, add, fam) => {
    console.log("err:  " + err, "add: " + add, "fam: " + fam);
});

The node.js dns.lookup documentation is at dns.lookup(hostname[, options], callback)

You might also find Katacoda Node.js Playground useful. You can use it to try out snippets of code. You can try this snippet from the node.js documentation in it and see what should be returned:

const dns = require('dns');
const options = {
  family: 6,
  hints: dns.ADDRCONFIG | dns.V4MAPPED,
};
dns.lookup('example.com', options, (err, address, family) =>
  console.log('address: %j family: IPv%s', address, family));

Be sure to change “example.com” to a real host name like “www.google.com”.


#3

But i want to check if a url is valid or not. I used this code.

let vaild = require('valid-url');

if(valid.isUri('www.google.com')
   console.log('yes');
else
   console.log('no');

But this show yes for many urls that are not correct.


#4

What do you mean by ‘not correct’ though, can you give an example?


#5

this is a valid url according to that code

htt:lordvoldemort.com 

It should be an invalid url as htt is not a protocol it’s http Or http

this is also a valid url
f:d.org