nodejs中的相对url到绝对url

时间:2017-08-21 13:31:20

标签: node.js relative-path absolute-path relative-url

我的网址是这样的:

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

我希望将其转换为绝对值,如下所示:

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

我正在寻找Nodejs中的模块来做同样的事情。 模块,路径,做同样的事情。但是(path.resolve)也会以目录路径为前缀。

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

1 个答案:

答案 0 :(得分:2)

您可以使用网址模块。 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: '' }
相关问题