Is this safe to use
It sounds like you want
url.resolve. From the Node docs:
url.resolve('/one/two/three', 'four') // '/one/two/four' url.resolve('http://example.com/', '/one') // 'http://example.com/one' url.resolve('http://example.com/one', '/two') // 'http://example.com/two'
As Andreas correctly points out in a comment,
url.resolve would only help if the problem is as simple as the example.
url.parse also applies to this question because it returns consistently and predictably formatted fields via the
URL object that reduces the need for "code full of ifs".