Search This Blog

Tuesday, January 14, 2014

HTTP URL Validity Check in Nodejs

Http URL Validity can be checked in 2 ways.
  • Using a regular expression to check if the pattern matches
  • Ping to that URL and see if there is a response or not i.e. if it exists or not.
Some times, the pattern matches but the the required URL may be or may not be existing. In order to check this in NodeJs, 'request' module can be used. Both the steps can be done in a single call.


  1. var request = require('request');
  2. request.head(srcUrl, function(err, resp) {
  3.      if(resp) {
  4.         console.log('URL Valid');
  5.         console.log(resp.headers);
  6.         var option = {
  7. uri: srcUrl,
  8. encoding: null
  9. };
  10.         request.get(option, function(err, resp, data) {
  11. if(!data){
  12. console.log('There is no data');
  13. } else {
  14. console.log(data);
  15.           }
  16.        }
  17.     } else {
  18.         console.log('Invalid URL');
  19.     }
  20. }
Let's discuss about the above line by line.

  • Line 1 is to include the node module 'request' into the current source.
  • Line 2 checks for the headers. It makes a request to the url to get only the headers but not the actual data.
  • Line 3 checks for a non-null response, it means that the URL is valid.
  • Line 5 prints the response headers. At this point, the headers can be checked for the content type, content-length etc.. and the respective business logic can be implemented.
    • Example: In order to check if the content is image, the following can be done
      • if (/image\/.*/.test(resp.headers['content-type'])) { console.log('Content is Image'); }
  • Line 10 makes a GET request to the URL and the data is retrieved. 


No comments:

Post a Comment