8

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.

2
  • 3
    Try path.normalize('http://example.com/path/to/css/../../images/test.jpg') Commented Aug 21, 2017 at 13:33
  • @ponury-kostek thanks. it does what i need. :) Commented Aug 23, 2017 at 7:42

1 Answer 1

13

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: '' }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.