Is there a replacement for url.resolve for creating URLs in Node?

Viewed 3353

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?

1 Answers

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.

Related