Can I use require(“path”).join to safely concatenate urls?

前端 未结 15 585
臣服心动
臣服心动 2020-12-23 13:06

Is this safe to use require(\"path\").join to concatenate URLs, for example:

require(\"path\").join(\"http://example.com\", \"ok\"); 
//returns          


        
15条回答
  •  难免孤独
    2020-12-23 13:42

    This is what I use:

    function joinUrlElements() {
      var re1 = new RegExp('^\\/|\\/$','g'),
          elts = Array.prototype.slice.call(arguments);
      return elts.map(function(element){return element.replace(re1,""); }).join('/');
    }
    

    example:

    url = joinUrlElements(config.mgmtServer, '/v1/o/', config.org, '/apps');
    

提交回复
热议问题