سؤال

I want to resolve an url with a port included. Concrete example:

mainServerURL   ='localhost:8080/';
tempFilesDir    ='/temp';

If I call url.resolve on these two variables, I get

'localhost:/temp'

But I'm actually looking for:

'localhost:8080/temp'

My code is pretty straightforward, so I'm afraid I must be using url.resolve wrong (due to theoretical errors on my side probably), but I've looked around and can't find another way of doing this.

Of course there's the string-way, but I'm guessing it's avoidable?

Any help is much appreciated!

هل كانت مفيدة؟

المحلول

The host of a URL can be denoted by having the prefix, //:

> var mainServerURL = '//localhost:8080/';
undefined
> var tempFilesDir  = '/temp';
undefined
> url.resolve(mainServerURL, tempFilesDir)
'//localhost:8080/temp'

Without it, localhost: is being treated as the protocol rather than the hostname:

> url.parse('localhost:8080/', false, true)
{ protocol: 'localhost:',
  host: '8080',
  port: null,
  hostname: '8080',
  ... }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top