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

前端 未结 15 536
臣服心动
臣服心动 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条回答
  •  萌比男神i
    2020-12-23 13:32

    My solution

    path.join(SERVER_URL, imageAbsolutePath).replace(':/','://');
    

    Edit: if you want to support windows enviroments

    path.join(SERVER_URL, imageAbsolutePath).replace(/\\/g,'/').replace(':/','://');
    

    The second solution will replace all the backslashes, so url parts like querystring and hash may be altered too, but the topic is joining just the url path, so I don't consider it an issue.

提交回复
热议问题