Latest web development tutorials

Node.js Web module

What is a Web server?

Web server generally refers to the site server resides on the Internet refers to some type of computer program, the basic functionality of a Web server is to provide Web browsing service information. It supports only HTTP protocol, HTML document format and URL, and the client's web browser with.

Most web servers support server-side scripting language (php, python, ruby), etc., and get data from the database via a script language, the results returned to the client browser.

The three most mainstream Web server is Apache, Nginx, IIS.


Web Application Architecture

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

  • Server - server, generally refers to the Web server, the client may receive a request, send a response to the client data.

  • Business - business layer, through the Web server processing applications, such as interact with the database, logic, calling external procedures.

  • Data - the data layer, generally composed by the database.


Create a Web server using Node

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

var http = require('http');

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

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


// 创建服务器
http.createServer( function (request, response) {  
   // 解析请求,包括文件名
   var pathname = url.parse(request.url).pathname;
   
   // 输出请求的文件名
   console.log("Request for " + pathname + " received.");
   
   // 从文件系统中读取请求的文件内容
   fs.readFile(pathname.substr(1), function (err, data) {
      if (err) {
         console.log(err);
         // HTTP 状态码: 404 : NOT FOUND
         // Content Type: text/plain
         response.writeHead(404, {'Content-Type': 'text/html'});
      }else{	         
         // HTTP 状态码: 200 : OK
         // Content Type: text/plain
         response.writeHead(200, {'Content-Type': 'text/html'});	
         
         // 响应文件内容
         response.write(data.toString());		
      }
      //  发送响应数据
      response.end();
   });   
}).listen(8081);

// 控制台会输出以下信息
console.log('Server running at http://127.0.0.1:8081/');

Next we create an index.htm file in the directory, as follows:

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

Executive server.js file:

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

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

Executive server.js console output is as follows:

Server running at http://127.0.0.1:8081/
Request for /index.htm received.     #  客户端请求信息

Gif examples demonstrate


Use Node Creating Web Client

Node Creating Web clients need to introduce http module, create client.js file, the code is as follows:

var http = require('http');

// 用于请求的选项
var options = {
   host: 'localhost',
   port: '8081',
   path: '/index.htm'  
};

// 处理响应的回调函数
var callback = function(response){
   // 不断更新数据
   var body = '';
   response.on('data', function(data) {
      body += data;
   });
   
   response.on('end', function() {
      // 数据接收完成
      console.log(body);
   });
}
// 向服务端发送请求
var req = http.request(options, callback);
req.end();

Open a new terminal, execute client.js file, the output results are as follows:

$ node client.js
<html>
<head>
<title>Sample Page</title>
</head>
<body>
Hello World!
</body>
</html>

Executive server.js console output information is as follows:

Server running at http://127.0.0.1:8081/
Request for /index.htm received.   # 客户端请求信息

Gif examples demonstrate