7

I read on this post that you can concatenate URLs using url.resolve:

url.resolve('http://example.com/', 'image.jpg')    // 'http://example.com/image.jpg'

But upon reading the documentation I see that this is now deprecated. Is there a replacement for url.resolve?

Andrew Einhorn
  • 741
  • 8
  • 23
  • 1
    "*Deprecated: Use the WHATWG URL API instead.*" - from the link you posted. – VLAZ Sep 30 '20 at 16:42
  • Sorry, this will sound stupid, but I couldn't find the function on the [WHATWG URL API](https://nodejs.org/api/url.html) documentation ... – Andrew Einhorn Sep 30 '20 at 16:50
  • 2
    `new URL(input[, base])` -> `new URL('/foo', 'https://example.org/'); // https://example.org/foo` - isn't that what you need? – VLAZ Sep 30 '20 at 16:51
  • 1
    Yes, that's exactly what I needed. I guess I got confused because new URL returns an object, not the string I was looking for. I then just had to grab the href using object.href. Thanks for your feedback here. – Andrew Einhorn Sep 30 '20 at 16:57

1 Answers1

12

Ok, so with a bit of help from the comments, I managed to work out the solution:

const url_object = new URL('image.jpg', 'http://example.com/')
const url_string = url_object.href  // http://example.com/image.jpg

Being a little new to this, I didn't realise I had to pull the href out of the object returned from new URL.

Andrew Einhorn
  • 741
  • 8
  • 23