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

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

    If you're using lodash, you can use this simple oneliner:

    // returns part1/part2/part3
    ['part1/', '/part2', '/part3/'].map((s) => _.trim(s, '/')).join('/')
    

    inspired by @Peter Dotchev's answer

提交回复
热议问题