在URL中获取正确路径

3
Node.js有一个URL模块可以解决这个问题吗?
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
1个回答

4
使用原生的URL模块,你需要使用url.resolve()函数。
url.resolve('http://www.domain.com/folder/folder/', '../img/some.jpg');
// http://www.domain.com/folder/img/some.jpg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接