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
答案 0 :(得分:4)
使用原生URL模块。你要找的是url.resolve()
。
url.resolve('http://www.domain.com/folder/folder/', '../img/some.jpg');
// http://www.domain.com/folder/img/some.jpg