user1710825
user1710825

Reputation: 588

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

Upvotes: 3

Views: 171

Answers (1)

hexacyanide
hexacyanide

Reputation: 91609

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

Upvotes: 4

Related Questions