Use node to build a local HTTP server. First understand the HTPP server principle
The HTTP protocol defines how Web clients request Web pages from a Web server and how the server routes Web pages to clients. the HTTP protocol uses the request /response model. The client sends a request message to the server that contains the requested method,URL, protocol version, request header, and request data. The server responds with a status line that includes the version of the Protocol, the success or error code, the server information, the response header, and the response data. This indicates the request /response model.
The following are the steps for HTTP request/response:
(1) client connects to Web server
An HTTP client, typically a browser, establishes a TCP socket connection with the HTTP port of the Web server (default is 80) . For example,http://www.oakcms.cn.
(2) sending an HTTP request
Through TCP sockets, the client sends a text request message to the Web server, which consists of a request line, a request header, a blank line, and 4 parts of the requested data .
(3) The server accepts the request and returns the HTTP response
The Web server resolves the request and locates the requested resource. The server writes a copy of the resource to the TCP socket, which is read by the client. A response consists of a status line, a response header, a blank line, and a 4 portion of the response data .
(4) Releasing the connection TCP connection
The Web server actively shuts down the TCP socket, releases the TCP connection, and the client passively shuts down the TCP socket, releasing the TCP connection.
(5) client browser parsing HTML content
The client browser parses the status line first to see the status code indicating whether the request was successful. Each response header is then parsed, and the response header informs the following character sets for several bytes of HTML documents and documents. The client browser reads the response data HTML, formats it according to the syntax of the HTML, and displays it in a browser window.
At present, the most mainstream three Web servers are Apache, Nginx
WEB Application Architecture
Client-clients, commonly referred to as browsers, can request data from the server via the HTTP protocol.
Server -a server-side, generally referred to as the Web servers, can receive client requests and send response data to the client.
Business-service layer, which handles applications through Web servers, such as interacting with databases, logical operations, calling external programs, and so on.
Data-the database layer, which is typically made up of databases.
Using node to implement a simple HTTP server is easy
1. Listening to the browser port (HTTP module)2. Get the requested URL (server-side file processing Gets the specified file fs file module)3. Return Data
Create a project directory
Create a new test.js in the project directory, write
varHttp=require ('http');//Open ServicevarServer=http.createserver (function (req,res) {Console.log ('have client connections');//Create connection successfully displayed in backgroundRes.writeheader ( $,{ 'Content-type':'text/html;charset= "Utf-8"' }); Res.write ('This is the body part .');//Display to clientres.end ();}). Listen (8888); Console.log ('server turned on successfully');
Run under the project
Node Server.js
Then enter 127.0.0.1:8888 on the browser side
Output
This is the body part.
Now you probably understand how to implement an HTTP server.
All right, let's go.
varHttp=require ('http');varFs=require ('FS');varroot="e:/hbspace/node/"//Open ServicevarServer=http.createserver (function (req,res) {varUrl=Req.url; varFile = root+URL; Fs.readfile (File,function (err,data) {if(Err) {Res.writeheader (404,{ 'Content-type':'text/html;charset= "Utf-8"' }); Res.write (''); Res.end (); }Else{Res.writeheader ( $,{ 'Content-type':'text/html;charset= "Utf-8"' }); Res.write (data);//display index.html on the clientRes.end (); }}). Listen (8888); Console.log ('server turned on successfully');
Introducing the FS module to read the file
Create a new index.html in the project directory write Hello World
Run Node Server.js
So easy a simple HTTP server is implemented
"Diary" to build a node local server