Module basic module

module-url

url processing module, the url module provides the Url class for processing URL-related operations, including parsing, assembling, splicing, etc.

Common properties and methods of the Url class are as follows:

Attributes:

  • href: Returns the full URL string.
  • protocol: The protocol part of the URL.
  • host: The host part of the URL.
  • auth: The authentication part of the URL.
  • hostname: The hostname portion of the URL.
  • port: The port portion of the URL.
  • pathname: The path portion of the URL.
  • search: The query parameter string for the URL.
  • hash: of the URLhashpart.

method:

  • resolve(from, to): resolve to (or the combination of to and from) into an absolute URL, and return the resolved URL object.
  • parse(urlString): Parses a URL string into a URL object and returns the object.
  • format(urlObject): Format the URL object into a URL string and return it.

Here is an example of a url module:

1 2 3 4 5 6 7 8 9 10 11
const { Url } = require('url'); const url = new Url('http://www.baidu.com/s?ie=UTF-8&wd=fibjs#hash'); console.log(url.protocol); // 'http:' console.log(url.host); // 'www.baidu.com' console.log(url.path); // '/s?ie=UTF-8&wd=fibjs' console.log(url.hash); // '#hash' console.log(url.href); // 'http://www.baidu.com/s?ie=UTF-8&wd=fibjs&query=fibjs#hash'

In this example, we demonstrate how to use the url module to parse a url string and reassemble it into a new url string.

object

URL

CreateUrlObjectrequest object, seeUrlObject

1
UrlObject url.URL;

static function

format

parameter constructionUrlObjectobject

1
static String url.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

return result:

  • String, returns a successfully constructed string

parse

parse a url string

1 2 3
static UrlObject url.parse(String url, Boolean parseQueryString = false, Boolean slashesDenoteHost = false);

Call parameters:

  • url: String, specifies the url string that needs to be parsed
  • 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'}

return result:

  • UrlObject, returns an object containing parsed data

resolve

Merge relative paths into one absolute path

1 2
static String url.resolve(String _from, String to);

Call parameters:

  • _from: String, source path
  • to: String, relative path

return result:

  • String, returns the resulting absolute path