Module basic module

module url

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

Commonly used properties and methods of the Url class are as follows:

Attributes:

  • href: Returns the complete 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 part of the URL.
  • port: The port part of the URL.
  • pathname: The path part of the URL.
  • search: The query parameter string of the URL.
  • hash: URL ofhashpart.

method:

  • resolve(from, to): will resolve to (or a combination of to and from) into an absolute URL and return the parsed URL object.
  • parse(urlString): Parses the 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 the 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 then 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 results:

  • String, returns the 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 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'}

Return results:

  • UrlObject, returns an object containing parsed data

resolve

Merge relative paths into an absolute path

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

Call parameters:

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

Return results:

  • String, returns the obtained absolute path

fileURLToPath

Convert a url object to a cross-platform relevant absolute path

1
static String url.fileURLToPath(UrlObject url);

Call parameters:

  • url:UrlObject, specify the url object that needs to be converted

Return results:

  • String, returns the converted absolute path

Convert a url string to a cross-platform relevant absolute path

1
static String url.fileURLToPath(String url);

Call parameters:

  • url: String, specifies the url string that needs to be converted

Return results:

  • String, returns the converted absolute path

pathToFileURL

Convert a cross-platform relevant absolute path to a url object

1
static UrlObject url.pathToFileURL(String path);

Call parameters:

  • path: String, specifies the absolute path to be converted

Return results:

  • UrlObject, returns the converted url object