What is a web server?
When an application (client) needs a certain resource, it can request the resource through HTTP to a server; the server that provides the resource is a Web server;
1.1 First experience with the server
1.2 Two ways to create a server
1.3 request object
The request object encapsulates all the information passed by the client to our server.
The URL of this request, the server needs to perform different processing according to different URLs;
the request method of this request, such as GET, POST request incoming The parameters and processing methods are different;
the headers of this request will also carry some information, such as client information, format for receiving data, supported encoding formats, etc...
Regular request:
When sending a request, the client will request different data, and then different request addresses will be passed in. The server needs to make different responses based on different request addresses.
If the user's request address also carries some additional parameters, how should we parse them?
We can use the url module.
Among them, the url module provides utility tools for URL processing and parsing.
Import url const url = require('url')
Suppose our request data is:
The result of the console parsing the url is:
Among them, pathname is the last path we need to obtain. Our purpose is to obtain the username and password separately in the query.
Import querystring module const qs = require('querystring');
const http = require("http")const url = require('url')const qs = require('querystring')// 1. Create a server const server = http .createServer((req, res) => { // Use built-in module const{ pathname,query } = url.parse(req.url) if(pathname === '/login'){ console.log(query); console.log(qs.parse(query)); const { username, password } = qs.parse(query) console.log(username,password); res.end('Request ends') }});// 2. Set the port number and start the server server.listen(8888,'0.0.0.0',()=>{ console.log("Server started successfully~");})
In the Restful specification (design style), we should use different request methods to add, delete, modify and check data:
We can Perform different processing by judging different request methods.
Assume that the following is our JSON request data in the body -> How to make our server obtain the username and password?
content-type is the type of data carried in this request:
** content-length: **The size and length of the file
keep-alive:
**accept-encoding: ** inform the server, the client The file compression format supported by the client, for example, js files can use gzip encoding, corresponding to .gz files
**accept:** Inform the server that the client can accept the file format type;
**user-agent:** Client-related information ;
1.4 response object
If we want to respond to the client with result data, we can do so in two ways:
note: if we do not call end and close, the client will wait for the result.
Http status code (Http Status Code) is a numeric code used to represent the Http response status:
Category | Reason Phrase | |
---|---|---|
1xx | Informational | The accepted request is being processed |
2xx | Success | The request was processed normally |
3xx | Redirection | Additional action is required to complete the request |
4xx | Client error Client | error Request error, the server cannot process the request |
5xx | Server Error (server error) | Server error in processing the request |
Common response codes:
Status code | description Status | description |
---|---|---|
200 | OK | Request successful. Generally used for GET and POST requests. |
400 | Bad Request is | a syntax error in the client request. The server cannot understand |
401 | Unauthorized | request requires user authentication. |
403 | Forbidden. | The server understands the client's request, but refuses to execute the request. |
404 | Not Found. | The server cannot process the request based on the client's |
Requests that a resource (web page) be found. Through this code, website designers can set up a personalized page of "The resource you requested cannot be found". | ||
500 | Internal Server Error. | The server has an internal error and cannot complete the request. |
503 | Service Unavailable | . Due to overload or system maintenance, the server is temporarily unable to process the client's request. The length of the delay can be included in the status code set in the server's Retry-After header |
:
returns header information. There are two main ways: