Your main problem is the URL validation. Since you’re only removing the https:// part of the URL, the DNS.lookup will also fail and return an error.
What you could do is this to validate the URL:
let parsedUrl;
try {
parsedUrl = new URL(req.body.url);
} catch (e) {
// If there's an error, then the URL is not valid
return req.json({error: 'invalid url'});
}
// Now you got a valid URL with more utility methods
// like the one used to retrieve only the hostname:
DNS.lookup(parsedUrl.hostname, ....);
@skaparate seems there’s another problem, I went by your solution but this time I am failing the last case, the case which checks for an invalid url all the other cases have been passed but the last one.