Join two URLs - Node.js String

Node.js examples for String:URL String

Description

Join two URLs

Demo Code

function joinURLs(url1, url2) {
  return removeTrailingSlash(url1) + "/" + removeLeadingSlash(url2);
}
function removeLeadingSlash(url) {
  return (url[0] == "/") ? url = url.substr(1) : url;
}
function removeTrailingSlash(url) {
  return (url[url.length - 1] == "/") ? url.substr(0, url.length - 1) : url;
}

Related Tutorials