Last Updated: February 25, 2016
·
642
· omarsmak

Parse custom pretty URL parameters in Node.Js (Javascript)

I was working on one of my private projects and I create pretty URL and parse the parameters in order to improve the SEO in the search engine. For example I wanted to parse the following URL format:
http://xxxxxxxx/:param1_to_:param2

The solution was pretty simple, I have created the following function which parses the parameters based on "to" separator (of course you can change that):

function(param){
      var myRe = '_to_';
      var params = param.split(myRe);
      var res;
      res = params.every(function(value, index){
         if(!value || 0 === value.length){
             //We are not intersted on values less than 2
             return false;
         } 
         return true;
      });

      if (res){
          return params;
      } else {
          return 0;
      }
     }

The above function will only return 2 parameters only