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

前端 未结 15 532
臣服心动
臣服心动 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:46

    When I tried PATH for concatenating url parts I run into problems. PATH.join stripes '//' down to '/' and this way invalidates an absolute url (eg. http://... -> http:/...). For me a quick fix was:

    baseurl.replace(/\/$/,"") + '/' + path.replace(/^\//,"") )
    

    or with the solution posted by Colonel Panic:

    [pathA.replace(/^\/|\/$/g,""),pathB.replace(/^\/|\/$/g,"")].join("/")
    

提交回复
热议问题