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)
- 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
If you still want a simple REGEX
Google is your friend. check this
source share