Check URL in Node.js

Hi, I want to check the URL of types www.google.com or http://www.google.com or google.com using a single reguar expression, is it possible, if so, to kindly share the solution in javascript. Please note that I expect the underlying protocols to be HTTP or HTTPS morover. The main question is how can we match all three of these patterns using one regular expression expression in Javascript, which should not check if this page is active or not, if the value entered by the user matches any of the above three cases, it should return true on the other hand, if it should not return fasle.

+8
source share
3 answers

Check live URL

This is a bit of a hack, but if I had to do this, I would approach it like this:

1st step

Parse and extract the domain / ip from a given URL

http: // drive.google.com / 0/23 ➡ drive.google.com

Here's how to do it in nodejs:

var url = require("url"); var result = url.parse('http://drive.google.com/0/23'); console.log(result.hostname); 

2nd step

ping the extracted domain / ip - not all servers will respond to ICMP (PING) requests due to network configuration.

 var ping = require ("net-ping"); var session = ping.createSession (); session.pingHost (target, function (error, target) { if (error) console.log (target + ": " + error.toString ()); else console.log (target + ": Alive"); }); 

3rd step

You can perform a HEAD HTTP request to this URL and check the status code.

 var request = require('request'); request({method: 'HEAD', uri:'http://www.google.com'}, function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body) // Show the HTML for the Google homepage. } }) 
  • A bit risky if it is a web service (since you can initiate actions).
  • It would be harder if the URL requires authentication / redirect
  • @Jan Jůna commented that it is better to use HEAD. He is absolutely right. Please note that not all web servers support the HEAD method.
  • Check Request Package

There is a package for this!

You can use an existing nodejs package named validUrl

using:

 var validUrl = require('valid-url'); var url = "http://bla.com" if (validUrl.isUri(url)){ console.log('Looks like an URI'); } else { console.log('Not a URI'); } 

Installation :

 npm install valid-url --save 

If you still want a simple REGEX

Google is your friend. check this

+26
source

The package "valid-url" npm did not work for me. It returned valid for an invalid url. What worked for me was "url-exists"

 const urlExists = require("url-exists"); urlExists(myurl, function(err, exists) { if (exists) { res.send('Good URL'); } else { res.send('Bad URL'); } }); 
+1
source

No need to use a third-party library .

To check if a string is a valid URL

  const URL = require("url").URL; const stringIsAValidUrl = (s) => { try { new URL(s); return true; } catch (err) { return false; } }; stringIsAValidUrl("https://www.example.com:777/a/b?c=d&e=f#g"); //true stringIsAValidUrl("invalid"): //false 

edit

If you need to limit the protocol to a number of protocols, you can do something like this

 const { URL, parse } = require('url'); const stringIsAValidUrl = (s, protocols) => { try { new URL(s); const parsed = parse(s); return protocols ? parsed.protocol ? protocols.map(x => '${x.toLowerCase()}:').includes(parsed.protocol) : false : true; } catch (err) { return false; } }; stringIsAValidUrl('abc://www.example.com:777/a/b?c=d&e=f#g', ['http', 'https']); // false stringIsAValidUrl('abc://www.example.com:777/a/b?c=d&e=f#g'); // true 
-1
source

All Articles