Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the functions of path, os and url modules in Node.js

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "what is the role of path, os and url modules in Node.js". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Path module in Node.js provides some path operation api,os module provides some information related to the operating system api,url core module provides us with api for parsing URL addresses. Today, we mainly learn about the common api of path module, os module and url module!

1. Path module (path)

Provide operation path information api

Path.extname (extension to get path information)

/ / introducing the path module let path = require ('path'); / / the extension let info = path.extname (' hello.html') console.log (info) for obtaining path information

Path.resolve (the sequence of paths or path fragments is resolved to an absolute path)

/ / resolve parses the sequence of a path or path fragment into an absolute path let arr = ['/ aaa','bbb','ccc'] let info1 = path.resolve (. Arr) / / array to deconstruct console.log (info1)

Path.join (concatenates path fragments using platform characteristic delimiters and normalizes the generated path)

/ / join uses platform characteristic delimiters to connect path fragments and standardize the generated path console.log (_ _ dirname); let info2 = path.join (_ _ dirname,'aaa','bbb','ccc') console.log (info2)

Here's a brief description of what these mean:

_ _ dirname: get the full directory name of the directory where the current execution file resides

_ _ filename: gets the file name of the current execution file with the full absolute path

Process.cwd (): gets the file directory name when the node command is currently executed

For more api, please see the official node documentation: http://nodejs.cn/api/path.html

2. System module (os)

Api that provides some information about the operating system

Os.cpus () (get cpu information)

Os.arch () (get system architecture: x32 or x64)

Os.totalmem () (get memory information)

.

For more api, please see the official node documentation: http://nodejs.cn/api/os.html

3. Url module

The url module provides utilities for URL processing and parsing. Two sets of API are provided to deal with URL: a legacy of the old version of API url.parse,url.format (), url.resolve (), and a new API that implements the WHATWG standard. It is recommended to use the new version and import the module by deconstructing the assignment.

Old edition

/ / Old version / / introduce url module let url = require ('url'); / / parse (url.parse) let urlMore = url.parse (' http://www.baidu.com?id=1&token=qwerty') / / old console.log (urlMore); / / url.resolve) let urlMore2 = url.resolve ('http://www.baidu.com','./aaa/ccc')console.log(urlMore2);)

New edition

/ / New version / / introduce url module let {URL} = require ("url"); / / pass in a complete absolute address let urlMore3 = new URL ('http://www.baidu.com?id=1&token=qwerty') / / new version of console.log (urlMore3)) / / the first parameter is passed in the relative path, and the second parameter is passed in the absolute path, and the two are spliced to analyze let urlMore4 = new URL ('. / ads/ddd',' http://www.baidu.com?')) Console.log (urlMore4)

Parameter resolution:

Hash: gets and sets the fragment part of the URL. Invalid URL characters contained in the value assigned to the hash property are percentage encoded.

Host: gets and sets the host part of the URL. (that is, the domain name plus port part).

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 ports.

Href: gets and sets the serialized URL. Getting the value of the href property 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 property is an invalid URL, a TypeError will be thrown.

Origin: contains the host of the protocol and gets the origin of the read-only serialized URL.

Port: Port gets and sets the port part of the URL. The port value can be a number or a numeric string in the range 0 to 65535 (inclusive). The port can be an empty string, and the port is automatically selected according to the protocol.

Protocol: set the connection protocol. Invalid protocol values will be ignored. Like http or https.

Search: gets and sets the serialization query part 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 URL with the url.search setting.

For more api, please see the official node documentation: http://nodejs.cn/api/url.html#urlresolvefrom-to

This is the end of the content of "what are the functions of path, os and url modules in Node.js". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report