在url中获取正确的路径

时间:2013-09-25 16:39:47

标签: javascript node.js url

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 个答案:

答案 0 :(得分:4)

使用原生URL模块。你要找的是url.resolve()

url.resolve('http://www.domain.com/folder/folder/', '../img/some.jpg');
// http://www.domain.com/folder/img/some.jpg
相关问题