Object UrlObject
UrlObject is an object used to represent URL information, we can conveniently use UrlObject object to represent and operate a URL address.
UrlObject encapsulates many useful methods and properties, such as parse and format, which can quickly parse and format URLs.
For UrlObject objects, we can create them in the following ways:
- useurlString to create UrlObject. In this method, parse automatically parses the URL, and if the URL is invalid, an exception will be thrown:
1
2
3
4const url = require('url');
const parsedURL = url.parse('https://www.google.com');
console.log(parsedURL);
- Create UrlObject with construction parameters:
1
2
3
4
5
6
7
8
9
10
11
12const url = require('url');
const parsedURL = new url.URL({
protocol: 'https:',
hostname: 'www.google.com',
pathname: '/search',
query: {
q: 'hello world',
}
});
console.log(parsedURL);
inheritance relationship
Constructor
UrlObject
UrlObject object constructor, constructed with parameters
1new UrlObject(Object args);
Call parameters:
- args: Object, a dictionary object specifying construction parameters, supported fields are: protocol, slashes, username, password, hostname, port, pathname, query,hash
UrlObject object constructor, useurlstring construction
1
2
3new UrlObject(String url = "",
Boolean parseQueryString = false,
Boolean slashesDenoteHost = false);
Call parameters:
- url: String, specifies the structureurlstring
- parseQueryString: Boolean, specifies whether to parse query
- slashesDenoteHost: Boolean, the default is false, if set to true, the string from the string '//' to the next '/' will be parsed as host, such as '//foo/bar', the result should be { host: 'foo', pathname: '/bar'} instead of {pathname: '//foo/bar'}
member attribute
href
String, query and set the current UrlObject object completeurlThe address description, which is assembled from all other attributes
1String UrlObject.href;
protocol
String, query and set the protocol name in the current UrlObject object
1String UrlObject.protocol;
slashes
Boolean, query and set whether the current UrlObject contains double slashes
1Boolean UrlObject.slashes;
auth
String, query and set the complete authentication string in the current UrlObject object, assembled from username and password properties
1String UrlObject.auth;
username
String, query and set the authenticated user in the current UrlObject object
1String UrlObject.username;
password
String, query and set the authentication password in the current UrlObject object
1String UrlObject.password;
host
String, query and set the complete host description in the current UrlObject object, assembled from hastname and port
1String UrlObject.host;
hostname
String, query and set the hostname in the current UrlObject object
1String UrlObject.hostname;
port
String, query and set the port number in the current UrlObject object
1String UrlObject.port;
path
String, query and set the full path of the request in the current UrlObject object (including the request), assembled by pathname and query
1String UrlObject.path;
pathname
String, query and set the path in the current UrlObject object
1String UrlObject.pathname;
search
String, query and set the request string (including "?") in the current UrlObject object, equivalent to "?"+query
1String UrlObject.search;
query
Value, query and set the request string in the current UrlObject object (excluding "?")
1Value UrlObject.query;
hash
String, query and set the request anchor in the current UrlObject object (including "#")
1String UrlObject.hash;
searchParams
HttpCollection, query the request string in the current UrlObject object (excluding "?")
1readonly HttpCollection UrlObject.searchParams;
member function
parse
parse aurlstring
1
2
3UrlObject.parse(String url,
Boolean parseQueryString = false,
Boolean slashesDenoteHost = false);
Call parameters:
- url: String, specifies what needs to be parsedurlstring
- parseQueryString: Boolean, specifies whether to parse query
- slashesDenoteHost: Boolean, the default is false, if set to true, the string from the string '//' to the next '/' will be parsed as host, such as '//foo/bar', the result should be { host: 'foo', pathname: '/bar'} instead of {pathname: '//foo/bar'}
format
Constructs a UrlObject with the specified parameters
1UrlObject.format(Object args);
Call parameters:
- args: Object, a dictionary object specifying construction parameters, supported fields are: protocol, slashes, username, password, hostname, port, pathname, query,hash
resolve
reseturlPath, automatically identify whether the new path is a relative path or an absolute path
1UrlObject UrlObject.resolve(String url);
Call parameters:
- url: String, specifies the new path
return result:
- UrlObject, returns an object containing the relocation data
normalize
standardized path
1UrlObject.normalize();
toString
Return the string representation of the object, generally return "[Native Object]", the object can be reimplemented according to its own characteristics
1String UrlObject.toString();
return result:
- String, returns a string representation of the object
toJSON
Returns the JSON format representation of the object, generally returns a collection of readable properties defined by the object
1Value UrlObject.toJSON(String key = "");
Call parameters:
- key: String, not used
return result:
- Value, which returns a JSON-serializable value