Is this safe to use require(\"path\").join
to concatenate URLs, for example:
require(\"path\").join(\"http://example.com\", \"ok\");
//returns
No, you should not use path.join()
to join URL elements.
There's a package for doing that now. So rather than reinvent the wheel, write all your own tests, find bugs, fix them, write more tests, find an edge case where it doesn't work, etc., you could use this package.
https://github.com/jfromaniello/url-join
npm install url-join
var urljoin = require('url-join');
var fullUrl = urljoin('http://www.google.com', 'a', '/b/cd', '?foo=123');
console.log(fullUrl);
Prints:
'http://www.google.com/a/b/cd?foo=123'