I am having trouble with dns.lookup() with some URLs (URL shortener project, finished it but trying to remove bugs). For example this URL, which is valid: https://guide.freecodecamp.org/jquery/jquery-ajax-post-method
I get this error message:
{ Error: getaddrinfo ENOTFOUND guide.freecodecamp.org/jquery/jquery-ajax-post-method
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)
errno: 'ENOTFOUND',
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'guide.freecodecamp.org/jquery/jquery-ajax-post-method' }
In my server.js file:
var regex = /^https?:\/\//; //need this format for res.redirect
if (regex.test(url)) {
var dnsUrl = url.slice(url.indexOf("//") + 2); //need to remove http(s):// to pass to dns.lookup
dns.lookup(dnsUrl, function(err, address, family) { //check for valid url
if (err) { console.log(err); }
else if (address !== undefined) {
console.log("address: " + address);
findOriginalUrl(url); //check to see if url exists in database
} else {
res.send("not a valid URL");
console.log("dnsUrl: " + dnsUrl);
}
}); //dns.lookup
} else {
res.send("invalid URL format");
}
});