What is a WEB server?
Web servers generally refer to Web servers, which are programs that reside on some type of computer on the Internet, and the basic function of Web servers is to provide web information browsing services. It only supports HTTP protocols, HTML document formats, and URLs, in conjunction with the client's Web browser.
Most Web servers support the server-side scripting language (PHP, Python, Ruby) and so on, and get the data from the database in a scripting language and return the results to the client browser.
At present, the most mainstream three Web servers are Apache, Nginx, IIS.
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.
Create a WEB server using Node
node. JS provides HTTP modules, which are used primarily to build HTTP servers and clients, using HTTP server or client features that must invoke the HTTP module with the following code:
var=require(' http ');
The following is a demonstration of one of the most basic HTTP server architectures (using 8081 ports) to create the Server.js file, as shown in the code below:
Varhttp= Require(' HTTP ');VarFs= Require(' FS ');Varwr.= Require(' URL ');Creating a serverhttp.Createserver( function (Request,Response) { Parse the request, including the file name VarPathname=Url.Parse(Request.Url).Pathname; The file name of the output requestConsole.Log("Request for" +Pathname+ "Received."); Read the requested file contents from the file systemFs.ReadFile(Pathname.Substr(1), function (Err,Data) { If (Err) {Console.Log(Err); HTTP Status code: 404:not FOUND Content Type:text/plainResponse.Writehead(404, {' Content-type ': ' Text/html '}); }Else{ HTTP Status Code: 200:ok Content Type:text/plainResponse.Writehead(200, {' Content-type ': ' Text/html '}); //response file contents Response. (data. Tostring } //send response data response< Span class= "pun". end (); }). listen (8081//console outputs the following information console. ( ' Server running at http://127.0.0.1:8081/'
Next we create a index.htm file in this directory with the following code:
<title>Sample Page</title><body >Hello world! </body>
Execute the server.js file:
$ node Server. JSServer running at http://127.0.0.1:8081/
Then we open the address in the browser: http://127.0.0.1:8081/index.htm, which appears as shown:
The console output information for executing server.js is as follows:
Server running at http://127.0.0.1:8081/Requestfor/index. HTM received. # Client Request Information
Create a WEB client using Node
Node creates a WEB client that needs to introduce an HTTP module to create a client.js file, as shown in the following code:
Varhttp= Require(' HTTP ');Options for the requestVarOptions= {Host: ' localhost ',Port: ' 8081 ',Path: '/index.htm ' };callback function for handling responsesVarCallback= function(Response){ Keep updating your data VarBody= ‘‘;Response.On(' Data ', function(Data) {Body+=Data; });Response.On(' End ', function () {//data receive complete Console.body}//send request to server req = Http. (options, callback req.
Open a new terminal, execute the client.js file, the output results are as follows:
$ node Client. Js<title>sample page </title></< Span class= "PLN" >head><body>< Span class= "Typ" >hello world! </body></< Span class= "PLN" >html>
The console output information for executing server.js is as follows:
Server running at http://127.0.0.1:8081/Requestfor/index. HTM received. # Client Request Information
node. JS Web Module