Http protocol in response to the request and

The composition of the site

Web applications can be divided into two parts: the client and server side.

Client: Part of running in a browser, is the user interface to see and interact with the program. Using HTML, CSS, JavaScript building.

Server: part of the operation in the server, responsible for storing data and processing the application logic.

Web server

Website Server: Web site provides access to services is the site server machine, it can receive the client's request , to make the request in response .

 Node Web server components:

1, computer

2, Node environment

3, the object request response

A few nouns server

IP addresses

Uniquely identify Internet devices.

IP is short for Internet Protocol Address, on behalf of the Internet protocol address

Local IP: 127.0.0.1

domain name

Because IP addresses difficult to remember, so its concept of a domain name, the domain name is usually called the Internet URL used .

http://www.itheima.com  =>  http://124.165.219.100/

Although the input of the URL in the address bar, but eventually will convert the domain name to ip to access to the specified site server.

The machine domain: localhost

port

Communication with the outside world is the computer port AC outlet, used to distinguish between different services provided in the server computer.

URL

Uniform Resource Locator, also known as URL (Uniform Resource Locator), is designed to identify the Internet online resource position one kind of addressing modes and set up, we usually refer to the web page address that is the URL.

Composition of the URL

Transfer Protocol: // server IP or domain name: port / location of the resources logo

http://www.itcast.cn/news/20181018/09152238514.html

http: hypertext transfer protocol, provides a method to publish and receive HTML pages.

Create a web server locally

1  // Create a web server module 
2  // reference system http module 
. 3 const = http the require ( "http" );
 . 4  
. 5  // create a web server 
. 6 const = App http.createServer ();
 . 7  
. 8  // When the server when there is a request 
9  // this is to add an event to the server, request on behalf of the request event is added, followed by the event handler 
10 app.on ( "request", (REQ, RES) => {
 . 11      // RES is response object 
12 is      // REQ is a request object 
13 is  
14     // request response to the client 
15     res.end ( '<h1 of> User Hello </ h1 of>' );
 16  });
 . 17 
18  // listening port, to provide services to the outside world 
19 app.listen (3000 );
 20  
21 console.log ( "The server started successfully");

HTTP protocol

HTPP protocols: Hypertext Transfer Protocol (English: HyperText Transfer Protocol, abbreviation: HTTP) defines how the server is transmitted hypertext to the local browser, which is based on client-server architecture work, is the client (user) and server ( website) request and response standards.

Packets

Data block transfer of HTTP requests and responses during the call in the packet, including data to be transmitted and additional information, and to comply with good form.

Request packet

Add the server request event when the second parameter is the event handler object that has two default parameters, the first parameter to a default for the  req request object

This object has three properties

req.headers // obtaining request packet

Obtaining request packet way

1      // Get Request message information 
2      // req.headers 
. 3      the console.log (req.headers);
 . 4      // obtaining a request packet in one [Key] 
. 5      the console.log (req.headers [ "Accept "]);
View Code

req.url // address acquisition request

Is the location of the resources acquired in the URL field

  Transfer Protocol: // server IP or domain name: port / location of the resources logo

  If resources are not the default location field acquisition   /

Different fields acquired different operations

1      // Get request address: is an identifier, to provide users with different responses 
2      //   req.url 
. 3      the console.log (req.url);
 . 4      IF (req.url == "/ index" || req.url == "/" ) {
 . 5          res.end ( "<H2> Welcome to the home </ H2>" );
 . 6      } the else  IF (req.url == "/ List" ) {
 . 7          res.end ( "available for purchase listpage to " );
 . 8      } the else {
 . 9          res.end (" Note Fond " );
 10      }
View Code

req.method // Get request method

Requesting divided into two types:

Address field enter the URL: GET Request The request for the data

POST request is a request to send data: send a post request form

. 1  <-! 
2  Method: Specifies the current form submission manner
 . 3  Action: Submit the specified address of the current form
 . 4  -> 
. 5  < form Method = "POST" Action = "HTTP: // localhost: 3000" > 
. 6      < INPUT type = "Submit" > 
. 7  </ form >
POST request

Different requests to do different operations

1 // 获取请求方式
2 // req.method
3 console.log(req.method);
4 if (req.method == "POST") {
5    res.end("post")
6 } else if (req.method == "GET") {
7    res.end("get")
8 }
View Code

Response message

 Set response packet

1      // response packet 
2      // res.writeHead () 
. 3      // Parameter 1: status code, do not write default is 200 
. 4      // Parameter 2: Object: to write the response packet information. The received packets do processing 
. 5      res.writeHead (200 is , {
 . 6          // set parsing code to HTML, coding format UTF8 
. 7          "Content-type": "text / HTML; charset = UTF8"
 . 8      });

status code

200 successful request

404 The requested resource is not found

500 server error

400 client requests a syntax error

Response content type of the message

text/html

text/css

application/javascript

image/jpeg

application/json

 

 

 

 

 

 

 

 

 

 

 

-------

 

Guess you like

Origin www.cnblogs.com/yzy-kyk/p/12608194.html