The Path module

How to know your current location and the location of the target file is the basis for operating on local files. (Do crawler in later stage, which can be used to download and store the downloaded resources)

The Path module introduces an object with many properties and methods inside.

Various common methods in the Path module

Path. join method: Used to join paths and join multiple paths into a single path. The main purpose of this method, as shown in the figure below, is to use the correct path separator on the current system, which is “/” on Unix and “\” on Windows.

__filename: the name of the directory where the current file is executed + the name of the file (absolute path). As shown below:

__dirname: Absolute path to the directory where the currently executed file resides. As shown below:

Path. resolve method: Used to concatenate paths and convert relative paths to absolute paths. It can take multiple arguments, one after the other, to indicate the path to be entered, all the way to the absolute path, along with the last argument. If the absolute path cannot be obtained based on the parameters, the current path is used as the baseline. Except for the root directory, the method returns values without trailing slashes. As shown in the figure below (the comment in the figure below should be changed to concatenate multiple addresses into one address, the original copy of the uplink comment forgot to change) :

Path.relative method: Used to concatenate paths and accept two parameters, both of which should be absolute paths. This method returns the relative path of the second path relative to the first path. As shown below:

Path. join, path.resolve, path.relative

If you simply want to concatenate paths, use path.join.

If you want to concatenate the absolute path, use path.resolve;

If you want to concatenate relative paths, use path.relative.

Path.parse () : Parses a directory or path. The result of parsing is an object that returns information about various parts of the path (the ext suffix and filename name are often used for judgment purposes and are important). As shown below:

Other methods in the Path module

Basename (path):path.basename(” pathaddr “, “filtterString”) returns the last part of path. Dirname is the directory name of the last level of the file.

Delimiter: Provides path delimiters specific to different platforms

Dirname (path): Returns the directory name of pathaddr

Extname (path): Returns the extension of path from the last occurrence of the. (period) character to the end of the string at the last part of path. An empty string is returned if there is no. In the last part of path, or if the basename of path (see path.basename()) is absent except for the first character.

Format (pathObject): The method returns the path string from the object. The opposite of path.parse().

IsAbsolute (path): checks whether a path isAbsolute. If path is an empty string, false is returned

Normalize (path): Normalize a given path, parse ‘.. ‘and ‘.’ fragments. When multiple consecutive path segment delimiters (\) are found, they are replaced with a single platform-specific path segment delimiter (\). The trailing delimiter is retained. If path is a zero-length string, a ‘.’ is returned to indicate the current working directory.

OS module (Understanding)

The OS module provides methods specific to the operating system. As shown below:

OS module attributes and methods

The os.EOL property is a constant that returns the newline character of the current operating system (\r\n on Windows, \n on other systems). Remember, this newline character is invisible, so we see the effect, as shown below:

The os.arch method: returns the current computer architecture. Possible values now are: ‘ARM’, ‘ARM64’, ‘ia32’, ‘MIPS’, ‘MIpsel’, ‘PPC’, ‘PPC64’, ‘S390’, ‘S390x’, ‘x32’, ‘x64’. As shown below:

Os.net workInterfaces() method: Returns an object containing only network interfaces assigned network addresses. As shown in the following two pictures (two pictures with too much content) :

The object that is assigned a network address contains the following properties:

Address

Assigned IPv4 or IPv6 address.

Netmask

IPv4 or IPv6 subnet mask.

Family

IPv4 or IPv6.

MAC

MAC address of the network interface.

Internal < Boolean > True if the network interface is loopback or similar remotely unavailable interface, false otherwise.

Scopeid

IPv6 digital domain identifier (available only if family is IPv6).

Cidr

IPv4 or IPv6 address with route prefix assigned in CIDR notation. If the netmask parameter is not available, this property is null.

Other interesting interfaces to OS modules

Platform () : Returns a string specifying the operating system platform on which Node.js was compiled

Release () : Returns an operating system version number

Cpus () : returns information about a logical CPU core

URL module

The URL module is used to generate and parse urls. This module must be loaded before use.

As shown above:

The URL module provides two sets of apis to handle urls: a legacy API from the older version and a new API that implements the WHATWG standard. The old API is part of the URL. Parse the new API is part of the URL

It is recommended to use only the new API, as shown below:

URL module URL parsing

Use method: new URL(input, base), can accept two parameters, but usually directly new URL(the URL address to parse)

Input

The absolute or relative URL path to parse. If input is a relative path, base is required. If input is an absolute path, base is ignored.

Base < string > | < URL > if the input is not an absolute path, it is to parse the base URL.

Hash: Gets and sets the fragment part of the URL. Invalid URL characters contained in the value assigned to the hash attribute are percent-encoded

Host: Gets and sets the host part of the URL. (Domain name plus port)

Url.hostname: Gets and sets the hostname part of the URL. The difference between url.host and url.hostname is that url.hostname does not contain a port

Href: Gets and sets the serialized URL. Getting the value of the href attribute is equivalent to calling url.tostring (). Setting the value of this property to a new value is equivalent to creating a new URL object using new URL(value). Each property of the URL object will be modified. If the value set to the href attribute is an invalid URL, TypeError will be raised.

Origin: the origin of a read-only serialized URL that contains the protocol host.

Port: Port Gets and sets the port portion of the URL. The port value can be a number or a string of numbers ranging from 0 to 65535. The port can be an empty string, in which case the port is automatically selected according to the protocol

Protocol: Sets the connection protocol. Invalid protocol values are ignored. Like HTTP or HTTPS

Search: Gets and sets the serialized query portion of the URL.

SearchParams: Gets the URLSearchParams object that represents the URL query parameters. This property is read-only. Replace the entire query parameter of the URL with the url.search setting

The queryString module

The QueryString module is mainly used to parse query strings.

Common methods of the queryString module

The querystring.parse() method is used to parse a querystring into a JavaScript object.

Parse (STR,sep,eq,options)

STR: query string to be parsed

Sep: Separator between multiple key-value pairs. Default is &

Eq: Separator between key name and key value, default =

The options: DecodeURIComponent is a function to restore the encoded string. The default is querystring.unescape(). By default, percent-encoded characters in the querystring are assumed to be utf-8 encoded. If other character encodings are used, other values need to be specified; The maxKeys attribute specifies the maximum number of attributes to parse. The default is 1000, and a value of 0 indicates an unlimited maximum number of attributes.

For example, we enter rixiang Ban46 in baidu’s search box.

You get a long string in the address bar:

https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&srcqid=5242299911359639549&tn=50000021_hao_pg&wd=%E6%97%A5%E5%90%91%E5%9D% 8246&oq=%25E7%25BE%258E%25E5%25B0%2591%25E5%25A5%25B3&rsv_pq=ca6dae6400172c40&rsv_t=623bfQGKDG%2FFpLdGBRROPunIGoLMXqmAef 8tfk0bCuArG1Q8UnN4BuCMZI4ms%2F472YBuDP%2BI&rqlang=cn&rsv_dl=tb&rsv_enter=1&rsv_btype=t&inputT=8720&rsv_sug3=13&rsv_sug1= 4&rsv_sug7=101&rsv_sug2=0&rsv_sug4=9517

We intercept what follows https://www.baidu.com/s?, pass it to queryString.parse (), and print it out. We get an object that stores data internally as name-value pairs, as shown in the following figure:

Querystring.stringify () method: this method can be interpreted as the reverse of parse’s method to convert an object into a queryString (for example, a set of data has been determined locally and is needed to combine the data into a request join)

The queryString.stringify() method can take a total of four arguments:

querystring.stringify(obj, sep, eq, options)

Obj: Target object to be composed

Sep: Separator between multiple key-value pairs. Default is &

Eq: Separator between key name and key value, default =

EncodeURIComponent: Function used to convert URL unsafe characters into percentage encoding in query strings. Default is QueryString.escape ().

For example, we use the queryString.stringify() method to combine the data into a request link, as shown below:

The difference between path, URL, queryString

Path resolves the local path

The URL resolves the network path

QueryString parses strings