我有这样的网址:
http://example.com/path/to/css/../../images/test.jpg
我想将其转换为绝对值,如下所示:
http://example.com/path/images/test.jpg
我正在 Nodejs 中寻找一个模块来做同样的事情。 模块 path 做同样的事情。但是(path.resolve)也以目录路径为前缀。
我正在寻找类似的东西,但是是为了 url。
最佳答案
您可以使用 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: '' }
关于node.js - nodejs 中相对 url 到绝对 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45798453/