7

I have url like this:

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

which i want to convert to absolute like the following:

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

I am looking for a module in Nodejs to do the same. The module, path, does the same thing. but(path.resolve) prepends with the directory path too.

I am looking for something similar, but for urls.

Learner
  • 315
  • 1
  • 5
  • 15

1 Answers1

12

You can use the URL module. 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: '' }
Pascal Lindelauf
  • 4,782
  • 4
  • 40
  • 55
posit labs
  • 8,951
  • 4
  • 36
  • 66