Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda