node.js ---- web server

What is a web server?

A web server generally refers to a website server, which refers to a program residing on a certain type of computer on the Internet. The basic function of a web server is to provide web information browsing services. It only needs to support the HTTP protocol, HTML document format and URL, and cooperate with the client's web browser.

Most web servers support server-side scripting languages ​​(php, python, ruby), etc., and obtain data from the database through the scripting language and return the results to the client browser.

At present, the three most mainstream Web servers are Apache, Nginx, and IIS.


Web Application Architecture

  • Client  - Client, generally refers to the browser, the browser can request data from the server through the HTTP protocol.

  • Server  - The server, generally refers to the Web server, can receive client requests and send response data to the client.

  • Business  - the business layer, which processes applications through the Web server, such as interacting with the database, logical operations, calling external programs, etc.

  • Data  - The data layer, generally consisting of databases.


Create a web server with Node

Node.js provides the http module. The http module is mainly used to build the HTTP server and client. To use the HTTP server or client functions, the http module must be called. The code is as follows:

var http = require('http');

The following is to demonstrate a basic HTTP server architecture (using port 8081), create a server.js file, the code is as follows:

var http = require('http');
var fs = require('fs');
var url = require('url');


// create server
http.createServer( function (request, response) {  
   // Parse the request, including the filename
   var pathname = url.parse(request.url).pathname;
   
   // output the requested filename
   console.log("Request for " + pathname + " received.");
   
   // Read the requested file content from the file system
   fs.readFile(pathname.substr(1), function (err, data) {
      if (err) {
         console.log(err);
         // HTTP status code: 404 : NOT FOUND
         // Content Type: text/plain
         response.writeHead(404, {'Content-Type': 'text/html'});
      }else{	         
         // HTTP status code: 200 : OK
         // Content Type: text/plain
         response.writeHead(200, {'Content-Type': 'text/html'});	
         
         // response file content
         response.write(data.toString());		
      }
      // send response data
      response.end();
   });   
}).listen(8081);

// The console will output the following information
console.log('Server running at http://127.0.0.1:8081/');

Next we create an index.htm file in this directory with the following code:

<html>
<head>
<title>Sample Page</title>
</head>
<body>
Hello World!
</body>
</html>

Execute the server.js file:

$ node server.js
Server running at http://127.0.0.1:8081/

Then we open the address in the browser: http://127.0.0.1:8081/index.htm, the display is as shown below:

The console output information of executing server.js is as follows:

Server running at http://127.0.0.1:8081/
Request for /index.htm received. # Client request information

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325672199&siteId=291194637