You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"protocol" is treated the same with or without the trailing : (colon).
The protocols http, https, ftp, gopher, file will be postfixed with :// (colon-slash-slash).
All other protocols mailto, xmpp, aim, sftp, foo, etc will be postfixed with : (colon).
"slashes" set to true if the protocol requires :// (colon-slash-slash)
Only needs to be set for protocols not previously listed as requiring slashes, such as mongodb://localhost:8000/.
// only the slashedProtocols get the //. Not mailto:, xmpp:, etc.
// unless they had them to begin with.
if (this.slashes ||
(!protocol || slashedProtocol[protocol]) && host !== false) {
host = '//' + (host || '');
if (pathname && pathname.charAt(0) !== '/') pathname = '/' + pathname;
} else if (!host) {
host = '';
}
So the documented behaviour only happens when host/hostname are set.
You can check this with the following snippet:
var url = require('url');
var unslashedUri = url.parse('/no-slashes');
var slashedUriByHost = url.parse('/slashes-host');
var slashedUriForced = url.parse('/slashes-forced');
unslashedUri.protocol = 'file';
console.log(url.format(unslashedUri)); // file:/no-slashes
slashedUriByHost.protocol = 'file';
slashedUriByHost.host = 'localhost';
console.log(url.format(slashedUriByHost)); // file://localhost/slashes-host
slashedUriForced.protocol = 'file';
slashedUriForced.slashes = true
console.log(url.format(slashedUriForced)); // file:///slashes-forced
I can make a PR correcting either the docs, or the url module, but which one is the intended behaviour?
The text was updated successfully, but these errors were encountered:
In the docs, at https://nodejs.org/api/url.html#url_url_format_urlobj it says:
This is not how the code works. If you look at https://github.com/nodejs/node/blob/master/lib/url.js you see:
So the documented behaviour only happens when host/hostname are set.
You can check this with the following snippet:
I can make a PR correcting either the docs, or the url module, but which one is the intended behaviour?
The text was updated successfully, but these errors were encountered: