const http = require('http')
const https = require('https')
let urlCdn = "https://baidu.com"
let urlObj = new URL(urlCdn)
const client = urlObj.protocol == 'https:' ? https : http
console.log(urlObj.protocol)
client.get(urlCdn, async (httpRes) => {
})
Node's http module: use node to create an htto server (key point)
1. Web server: a website server, mainly providing browsing services for online information
2. Web resources
(1) Static resources: The server has not been modified, and the result of each client request is the same resource. (CSS, images, etc.)
(2) Dynamic resources: resource information processed by the server
3. HTTP protocol: HyperText Transfer Protocol, hypertext transfer protocol. Standardizes the data format for interaction between the client and the server. It is a protocol based on 'request' - 'response'
(1) Request (request): The client (browser) sends information to the server (send request) -- http request
(2) Response: The response made by the server after receiving the client's request -- http response
(3) Content:
Basic information: request address (url), request method (get/post), request-response completion, routing address (ip address)
Response header image: the version number of the http protocol, 200 is the status code (indicating that the request-response has been completed), and the format of the response text
Request header information: User-Agent (the kernel of the client browser), Host (the address and port number of the requested server), and the text format of the Accept request
(4) Status code: Different status codes reflect the completion of the request-response process
200: The request-response process has been successfully completed
204: The request-response has been completed, but there is no response data
A status code starting with 3 indicates that the page was redirected
404: The resource requested by the client does not exist
403: The server rejected the request
400: Request syntax error
500: Server error, unable to respond to request
503: The server is unavailable
(5) Format of response information: defined in the way of 'large type/specific type'
text/plain: plain text format
text/html: html file
text/css: css file
application/javascript: js file
4. Application of http module in node:
(1) Import: require('http')
(2) Create a server: http.createServer(function(){ }), return an http server object
(3) Start server listening: listen (port number, server address, callback)
5. The way to obtain the data requested by the client on the server side: it is done through the request object request
(1) req.method: Get the request method of the client
(2) req.url: Get the request address of the client
(3) req.url.query: Get the request data sent by the client to the server by means of get
Request data sent by get method: it is spliced after the url address
http://127.0.0.1:9000?userName=Zhang San&age=25
'?': is the separator between url and request parameter (query)
'&': is the separator between parameters
(4) req.body: Get the request data sent by the client to the server in post mode
When sending a request in post mode: first send the request address separately, and then bind the request parameters and the body of the page together for sending
On the http server side of node, you need to use the querystring module to convert the body information sent by the client to get the request parameters
Reference link: