Is this safe to use require("path").join
to concatenate urls, for example:
require("path").join("http://example.com", "ok");
//returns 'http://example.com/ok'
require("path").join("http://example.com/", "ok");
//returns 'http://example.com/ok'
If not, what way would you suggest for doing this without writing code full of ifs.
If you're using lodash, you can use this simple oneliner:
inspired by @Peter Dotchev's answer
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.
url-join
https://github.com/jfromaniello/url-join
Install
npm install url-join
Usage
Prints:
'http://www.google.com/a/b/cd?foo=123'
This is what I use:
example: