相对url到绝对url的nodejs

我有这样的url:

http://example.com/path/to/css/../../images/test.jpg

我想要转换为绝对如下所示:

http://example.com/path/images/test.jpg

我在Nodejs中寻找一个模块来做同样的事情。 模块path做同样的事情。 但是(path.resolve)也会预设目录path。

我正在寻找类似的东西,但对于网站。

您可以使用URL模块。 https://nodejs.org/docs/latest/api/url.html

 const { URL } = require('url'); new URL('path/images/test.jpg, 'http://example.com/') URL { href: 'http://example.com/path/images/test.jpg', origin: 'http://example.com', protocol: 'http:', username: '', password: '', host: 'example.com', hostname: 'example.com', port: '', pathname: '/path/images/test.jpg', search: '', searchParams: URLSearchParams {}, hash: '' } 
Interesting Posts