A URL string is a structured string made up of several meaningful parts. In our work, we inevitably will use one of the parts, the most primitive by clipping and regular string matching method is hard to avoid will not be very convenient to use and beautiful, so in our nodejs provides a processing modules and parsing the url url, which provides some practical function enables us to parse more convenient and quick, Now let’s look at the common functions it provides

The URL module provides two sets of apis for handling URLs: one is the legacy API of Node.js,

Reason for retention: While the legacy node.js specific apis are not deprecated, they are reserved for backward compatibility with existing applications. So new applications use the WHATWG API.

The other is an API that implements the WHATWG URL Standard, usually in a Web browser. The API is officially implemented in Node8.0.0

In browsers, WHATWG urls are always available globally, whereas in Node.js, any case of opening or using a link must first reference the ‘URL’ module: require(‘ URL ‘).url

const url = require('url');
Copy the code
Let’s start by looking at what methods are available in this module.
let http = require('http');
let url = require('url');
console.log(url);

// { Url: [Function: Url],
//     parse: [Function: urlParse],
//     resolve: [Function: urlResolve],
//     resolveObject: [Function: urlResolveObject],
//     format: [Function: urlFormat],
//     URL: [Function: URL],
//     URLSearchParams: [Function: URLSearchParams],
//     domainToASCII: [Function: domainToASCII],
//     domainToUnicode: [Function: domainToUnicode] }
Copy the code
Let’s take a look at each of these methods one by one
let {parse, resolve, format, URL, URLSearchParams, domainToASCII, domainToUnicode} = require('url');
Copy the code
1, parse (urlStr, the queryString, AnalysisHost)

Node.js legacy unique API parameters:

UrlStr: indicates the url to be parsed. QueryString: indicates the parsed queryString or query object. True is an object. Query: {a: 123}}; query: ‘a=123’; Whether to resolve host (the string after // to the next/before), e.g. //foo/bar will be resolved as {host: ‘foo’, pathname: ‘/bar}, otherwise {pathName: ‘//foo/bar’}. The default is false

Parses the URL and returns a URL property object

Such as:

const myURLA =
    url.parse('https://user:[email protected]:8080/p/a/t/h?query=string#hash'.true);
console.log(myURLA);

// Url {
//     protocol: 'https:'// slashes:true,
//         auth: 'user:pass'// username and password // host:'sub.host.com:8080'// host host name // port:'8080'// port number // hostname:'sub.host.com'// Host name without port number //hash: '#hash', // hash value // search:'? query=string'// query string // query:'query=string'// Request parameters // pathName:'/p/a/t/h'// pathname // path:'/p/a/t/h? query=string'// the path name with query // href:'https://user:[email protected]:8080/p/a/t/h?query=string#hash'// The string itself}Copy the code

Error:

TypeError will be raised if urlStr is not a string.

const myurl = url.parse({a:123});
TypeError: Parameter "url" must be a string, not object
Copy the code

Raises URIError if the auth attribute exists but cannot be encoded.

2, resolve the from and to)

Parameters:

From: basic URL for parsing. To: url of the hyperlink to be parsed

What it does: Parses a target URL relative to a base URL in a way that a Web browser parses hyperlinks.

Such as:

const url = require('url');
url.resolve('/one/two/three'.'four');         // '/one/two/four'
url.resolve('http://example.com/'.'/one');    // 'http://example.com/one'
url.resolve('http://example.com/one'.'/two'); // 'http://example.com/two'
Copy the code
3, the format (url, options)

Parameters:

Url: a WHATWG URL object options: 1.auth: True if the serialized URL string should contain the username and password, false otherwise. The default is true. Fragment: True if serialized URL string should contain fragment, false otherwise. The default is true. Search: If the serialized URL string should contain the search query true, otherwise false. The default is true. Unicode characters that appear in the URL string host element should be encoded directly instead of using Punycode to encode true. Default is false. Returns a customizable serializable URL string representation of a WHATWG URL object.

Although the URL object’s toString() method and href attribute can both return a serialized string of the URL. However, neither can be customized. The url.format(url [, options]) method allows basic customization of the output.

Such as:

const { URL } = require('url');
const myURL = new URL('https://a:b @hello Hello? abc#foo'); console.log(myURL.href); / / output https://a:b@xn - 6 qqa088eba /? abc#fooconsole.log(myURL.toString()); / / output https://a:b@xn - 6 qqa088eba /? abc#foo

console.log(url.format(myURL, { fragment: false, unicode: true, auth: false})); / / output'https:// hello hello /? abc'
Copy the code
4, new URL(input[, base])

Browser-compatible URL class, implemented according to the WHATWG URL standard.

Note: By browser convention, all properties of the URL object are implemented as getters and setters on the prototype of the class, not as data properties of the object itself. Therefore, unlike [legacy urlObjects][], using the delete keyword on any property of a URL object (such as delete myurl.protocol, delete myurl.pathname, etc.) has no effect, but still returns true.

Parameters:

Input: indicates the input URL to be parsed. Base: Indicates the base URL to be parsed if input is a relative URL

Creates a new URL object by parsing the input onto base. If base is a string, parsing is the same as new URL(base).

Such as:

const { URL } = require('url');
const myURL = new URL('/foo'.'https://example.org/');
  // https://example.org/foo
Copy the code

TypeError will be raised if the input or base URLs are invalid. Note that the given value is cast to a string. Such as:

const { URL } = require('url');
const myURL = new URL({ toString: () => 'https://example.org/' });
  // https://example.org/
Copy the code

Unicode characters present in the input host name are automatically converted to ASCII using the Punycode algorithm.

const { URL } = require('url');
const myURL = new URL('https:// Hello hello ');
  // https://xn--6qqa088eba/
Copy the code
5.URLSearchParams

The URLSearchParamsAPI interface provides read and write permissions on the URLquery part. The URLSearchParams class can also be used alone with any of the following four constructors.

Such as:

const { URL, URLSearchParams } = require('url');

const myURL = new URL('https://example.org/?abc=123');
console.log(myURL.searchParams.get('abc')); / / output 123 myURL) searchParams) append ('abc'.'xyz'); console.log(myURL.href); / / output https://example.org/?abc=123&abc=xyz myURL. SearchParams. Delete ('abc');
myURL.searchParams.set('a'.'b'); console.log(myURL.href); https://example.org/?a=b const newSearchParams = new URLSearchParams(myurl.searchparams); Const newSearchParams = new URLSearchParams(myurl.search); // Const newSearchParams = new URLSearchParams(myurl.search); newSearchParams.append('a'.'c'); console.log(myURL.href); https://example.org/?a=b console.log(newSearchparams.tostring ()); // a=b&a=c // newSearchParams.toString() is implicitly called myurl.search = newSearchParams; console.log(myURL.href); https://example.org/?a=b&a=c newsearchparams.delete ('a'); console.log(myURL.href); Output / / https://example.org/?a=b&a=cCopy the code
6, domainToASCII (domain)

Punycode ASCII serialized domain. Empty string if domain is invalid. It performs the reverse of url.domaintounicode ().

const url = require('url');
console.log(url.domainToASCII('español.com')); // Export xn--espaol-zwa.com console.log(url.domaintoascii ('Chinese. Com')); Xn --fiq228c.com console.log(url.domaintoascii ('xn - invalid.com)); // Outputs an empty stringCopy the code
7. domainToUnicode(domain)

Returns the Unicode serialized domain. If the domain is invalid, an empty string is returned.

It performs the reverse of url.domaintoASCII ().

const url = require('url');
console.log(url.domainToUnicode('xn--espaol-zwa.com')); // espanol.com console.log(url.domaintounicode ('xn--fiq228c.com')); Log (url.domaintounicode ('xn - invalid.com)); // Outputs an empty stringCopy the code