'Safest way to validate URL in Node natively?

I have a JS app that needs to validate URLs. I've seen posts that suggest me to use libraries or regex, but this is not what I am looking for.

I want to know if there is a native methods that validate URLs (no problem if only check http and https protocols).

I've tried the dns module

    dns.lookup(<MY_URL>, {}, (err) => {
      if (err) { 
        /* do something */
      } else {
        /* do something else */
      }
    }

but seems to not work in every case.



Solution 1:[1]

There are the various things you can check on the URL.

  1. You can check that it's a properly formed URL that has all the expected parts. If it's not properly formed, then it's not a valid URL, but just because it's properly formed does not mean it's actually a working URL.

  2. If it passes the first test, you can parse the URL, get the host and see if the host exists in DNS. If it doesn't exist in DNS, then probably not a working URL.

  3. If it exists in DNS, you can probe the host on whatever port is present or implied from the URL and see if you can get any response. Assuming this is an http(s) URL, you can try to do a GET / and see if you get any sort of connection and/or network response. If you do, then an http(s) host does apparently exist at that domain/protocol/port. If you get a connection error, then it would appear that the host is not operating.

  4. If you expect the URL to be something that works for a GET request, then you can do a GET on the full path of the URL and see if you get a 2xx or 3xx response. If you do, then you appear to have an operating URL. If you don't, then apparently not.

  5. If you don't know if the URL is supposed to work for a GET, then checking the host for any response (as in step 3) is really all you can do because you won't be able to conclude anything by testing a GET of the whole path.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jfriend00