Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!