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

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

    By the time posting this answer url.resolve() is deprecated;

    I did following to join to path in Nodejs:

    const path = require('path');
    const url = require('url');
    
    
    let myUrl = new URL('http://ignore.com');
    myUrl.pathname=path.join(firstpath, secondpath);
    console.log(myUrl.pathname)
    

    This approach logs correct url path and it works for my case.

    What is your opinion about this approach?

    Thanks

提交回复
热议问题