In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use the path module, url module and http module in Node.js. I believe most people don't know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's learn about it together.
Path module
In Node.js, a path module is provided. In this module, many practical methods and properties that can be used to handle and transform file paths are provided. Path is a system module that does not need to be installed separately and is mainly used to format or splice the full path.
1. Example of path module
Take the most commonly used join method [stitching path] in the path module as an example, as shown below:
Var path = require ('path'); / / stitching path: concatenate multiple strings into a complete path var file = path.join (_ _ dirname,'file1.txt'); console.log ("current path:" + file)
Example screenshot, as shown below:
Note: _ _ dirname is a built-in property that represents the path where the current program is located.
2. Other methods of path module
The path module provides other methods in addition to join, as follows:
Var path = require ('path'); / / concatenate multiple strings into a complete path var file = path.join (_ _ dirname,'file1.txt'); console.log ("current path:" + file); / / return the folder part in the path var dirname = path.dirname (file); / / return the file part in the path, including the file name and extension var filename = path.basename (file) / / return the extension var extname = path.extname (file) in the path; / / parse the path object and return an object var p = path.parse (file); console.log ("dirname:" + dirname); console.log ("filename:" + filename); console.log ("extname:" + extname); console.log ("parse parsed object:"); console.log (p)
Example screenshot, as shown below:
Url module
The URL module mainly provides properties and methods for the related operations of URL (Uniform Resource Locator, uniform Resource Locator).
A URL string is a structured string that contains multiple meaningful components. When parsed, the URL object containing the properties of each component is returned.
The url module provides two types of API for dealing with URLs: an old version of API specific to Node.js, and a new version of API that implements the same WHATWG URL standard used by Web browsers.
A comparison between WHATWG and older API is provided below.
Use WHATWG API to parse the URL string:
Const myURL = new URL ('https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash');
Parse the URL string using the old version of API:
Import url from 'url'; const myURL = url.parse (' https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash');1. The old analytical method
The old parsing method can simply use the module's parse function, as shown below:
Var url = require ('url'); var u = "https://www.cnblogs.com/hsiang/p/15182972.html"; / / the old parsing method var obj = url.parse (u); console.log (" parsed object: "); console.log (obj)
Parse the sample screenshot, as shown below:
Note: the obj returned is an object, via obj. Property name, you can get more detailed content
two。 New parsing method / / New parsing method [ES6 Writing] const {URL} = require ('url'); var u = "https://www.cnblogs.com/hsiang/p/15182972.html"; const obj=new URL (u); console.log (" New parsing method "); console.log (obj)
Example screenshot, as shown below:
ES5 is written as follows:
/ / New parsing method [ES5 Writing] var url = require ('url'); var u = "https://www.cnblogs.com/hsiang/p/15182972.html"; var obj = new url.URL (u); console.log (" New parsing method "); console.log (obj)
Note: through the comparison, it is found that the results of the new and old methods are basically the same, with only slight differences. Please refer to the above screenshot of the new and old comparison.
Http module
As a virtual platform for information transmission, reception and sharing, the network connects the information of various points, faces and bodies together, so as to realize the sharing of these resources. There are certain rules for network data transmission, which are called protocols. Http protocol is one of the rules, and it is one of the most frequently used network transmission protocols.
1. What is the HTTP protocol?
Hypertext transfer Protocol (Hyper Text Transfer Protocol,HTTP) is a simple request-response protocol that usually runs on top of TCP. It specifies what kind of message the client might send to the server and what kind of response it gets. The headers of the request and response messages are given in ASCII form; the message content has a MIME-like format. The HTTP protocol defines the data format and process of data transfer between the browser and the server over the network.
2. Details of HTTP protocol constraints
The HTTTP protocol defines the format and process of data exchange between the browser and the server. The details are as follows:
Defines the format in which the browser sends requests to the server
Defines the format in which the server parses the data sent by the browser
Defines the format in which the server responds to the data to the browser
Defines the format in which the browser parses the data of the server response
3. HTTP request response process
Take a visit to a web page as an example, the request response process is as follows:
4. Http module get method
Because most of the requests in the network are in get mode and without the request body, the http module of Node.js provides a convenient method. As follows:
Var http=require ('http'); var fs = require (' fs'); http.get ('http://www.itsource.cn',function(res){ / / res is an IncomingMessage object / / console.log (res); var stream = fs.createWriteStream ('. / a.html'); / / res is a stream object and can listen for data event res.on ('data',function (data) {console.log (data.toString () Stream.write (data);}); res.on ('end',function () {stream.end (); console.log (' download successful');}); / / or use pipeline / / res.pipe (stream);})
The above example uses the get method to get the contents of the file and save the contents to the file. The example results are as follows:
These are all the contents of this article entitled "how to use path module, url module and http module in Node.js". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.