Object built-in object

ObjectUrlObject

UrlObject is an object used to represent URL information. We can easily use UrlObject objects to represent and operate a URL address.

UrlObject encapsulates many useful methods and properties, such as parse and format methods, which can quickly parse and format URLs.

For UrlObject objects, we can create them in the following ways:

  1. useurlStringCreateUrlObject. In this method, parse automatically parses the URL. If the URL is illegal, an exception will be thrown:
1 2 3 4
const url = require('url'); const parsedURL = url.parse('https://www.google.com'); console.log(parsedURL);
  1. Create UrlObject using construction parameters:
1 2 3 4 5 6 7 8 9 10 11 12
const 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 using parameters

1
new 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 3
new UrlObject(String url = "", Boolean parseQueryString = false, Boolean slashesDenoteHost = false);

Call parameters:

  • url: String, specified constructorurlstring
  • parseQueryString: Boolean, specifies whether to parse the query
  • slashesDenoteHost: Boolean, defaults to false, if set to true, the string after the string '//' and before 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 properties

href

String, query and set the complete string in the current UrlObject objecturlThe address description, which is assembled from all other properties

1
String UrlObject.href;

protocol

String, query and set the protocol name in the current UrlObject object

1
String UrlObject.protocol;

slashes

Boolean, query and set whether the current UrlObject object contains double slashes

1
Boolean UrlObject.slashes;

auth

String, query and set the complete verification string in the current UrlObject object, assembled from the username and password attributes

1
String UrlObject.auth;

username

String, query and set the authenticated user in the current UrlObject object

1
String UrlObject.username;

password

String, query and set the verification password in the current UrlObject object

1
String UrlObject.password;

host

String, query and set the complete host description in the current UrlObject object, assembled by hastname and port

1
String UrlObject.host;

hostname

String, query and set the host name in the current UrlObject object

1
String UrlObject.hostname;

port

String, query and set the port number in the current UrlObject object

1
String UrlObject.port;

path

String, query and set the full request path (including request) in the current UrlObject object, assembled from pathname and query

1
String UrlObject.path;

pathname

String, query and set the path in the current UrlObject object

1
String UrlObject.pathname;

String, query and set the request string (including "?") in the current UrlObject object, equivalent to "?" + query

1
String UrlObject.search;

query

Value, query and set the request string in the current UrlObject object (excluding "?")

1
Value UrlObject.query;

hash

String, query and set the request anchor point in the current UrlObject object (including "#")

1
String UrlObject.hash;

searchParams

HttpCollection, Query the request string in the current UrlObject object (excluding "?")

1
readonly HttpCollection UrlObject.searchParams;

member function

parse

Parse aurlstring

1 2 3
UrlObject.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 the query
  • slashesDenoteHost: Boolean, defaults to false, if set to true, the string after the string '//' and before 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 UrlObject with specified parameters

1
UrlObject.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

1
UrlObject UrlObject.resolve(String url);

Call parameters:

  • url: String, specify the new path

Return results:

  • UrlObject, returns an object containing relocation data

normalize

standardized path

1
UrlObject.normalize();

toString

Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.

1
String UrlObject.toString();

Return results:

  • String, returns the string representation of the object

toJSON

Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.

1
Value UrlObject.toJSON(String key = "");

Call parameters:

  • key: String, not used

Return results:

  • Value, returns a value containing JSON serializable