In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about the usefulness of the http module in Node.js. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
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:
Thank you for reading! This is the end of the article on "what is the use of the http module in Node.js?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.