Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get correct path in url

Does Node.js have a URL module to resolve this?

getCorrectUrl('http://www.domain.com/folder/folder/', '../../img/some.jpg'); // http://www.domain.com/img/some.jpg
getCorrectUrl('http://www.domain.com/folder/folder/', './img/some.jpg'); // http://www.domain.com/img/some.jpg
getCorrectUrl('http://www.domain.com/folder/folder/', 'http://www.domain.com/img/some.jpg'); // http://www.domain.com/img/some.jpg
getCorrectUrl('http://www.domain.com/folder/folder/', '../img/some.jpg'); // http://www.domain.com/folder/img/some.jpg
getCorrectUrl('http://www.domain.com/folder/folder/', 'img/some.jpg'); // http://www.domain.com/folder/folder/img/some.jpg
like image 731
user1710825 Avatar asked Jul 31 '26 03:07

user1710825


1 Answers

Use the native URL module. What you'd be looking for is url.resolve().

url.resolve('http://www.domain.com/folder/folder/', '../img/some.jpg');
// http://www.domain.com/folder/img/some.jpg
like image 110
hexacyanide Avatar answered Aug 01 '26 16:08

hexacyanide