URL Shortener Functional Problem

Using the dns.lookup() function to verify an inputted URL is not working properly for me. While a URL with just a hostname works (for example https://google.com or http://nfl.com), when there are things added after the hostname (like https://google.com/imghp or anything else with added arguments using /), the dns lookup callback returns an address and family of undefined. Why is this and how can I fix it?

Here is my code for verifying a URL:

dns.lookup(url, function(err, address, family) {
console.log(“ADDRESS: %j FAMILY: IPv%s”, address, family);
if (address == undefined) {
res.json({ error: “invalid url” });

The dns.lookup method should be used to lookup only the domain part of the url.

For me when I tried adding the http protocol e.g https:// or http://, or appending anything to the end of the domain e.g freecodecamp.org/learn, I got the same error as below.

The address and family return undefined, while the error returns errno: -3008, code: “ENOTFOUND”, ....

Hope this solves your problem.

1 Like