An easy solution to a common problem.
Tested with Node.js LTS (16 at the time of the gist creation).
buildUrl('http://example.com', 'a', 'b', 'c').toString() // http://example.com/a/b/cNote that the function returns a URL object, not a string.
This gist is mostly inspired by a post on StackOverflow.