本節(jié)介紹Node.js Web模塊,首先,你應(yīng)該先了解什么是Web服務(wù)器。


什么是 Web 服務(wù)器?

Web服務(wù)器一般指網(wǎng)站服務(wù)器,是指駐留于因特網(wǎng)上某種類型計(jì)算機(jī)的程序。

Web服務(wù)器的基本功能就是提供Web信息瀏覽服務(wù)。它只需支持HTTP協(xié)議、HTML文檔格式及URL,與客戶端的網(wǎng)絡(luò)瀏覽器配合。

大多數(shù)web服務(wù)器都支持服務(wù)端的腳本語言(php、python、ruby)等,并通過腳本語言從數(shù)據(jù)庫獲取數(shù)據(jù),將結(jié)果返回給客戶端瀏覽器。

目前最主流的三個(gè)Web服務(wù)器是Apache、Nginx、IIS。


Web 應(yīng)用架構(gòu)

  • Client - 客戶端,一般指瀏覽器,瀏覽器可以通過HTTP協(xié)議向服務(wù)器請求數(shù)據(jù)。

  • Server - 服務(wù)端,一般指Web服務(wù)器,可以接收客戶端請求,并向客戶端發(fā)送響應(yīng)數(shù)據(jù)。

  • Business - 業(yè)務(wù)層, 通過Web服務(wù)器處理應(yīng)用程序,如與數(shù)據(jù)庫交互,邏輯運(yùn)算,調(diào)用外部程序等。

  • Data - 數(shù)據(jù)層,一般由數(shù)據(jù)庫組成。


使用 Node 創(chuàng)建 Web 服務(wù)器

Node.js提供了http模塊,http模塊主要用于搭建HTTP服務(wù)端和客戶端,如果要使用HTTP服務(wù)器或客戶端功能,則必須調(diào)用http模塊,代碼如下:

var http = require('http');

以下是演示一個(gè)最基本的HTTP服務(wù)器架構(gòu)(使用8081端口),創(chuàng)建server.js文件,代碼如下所示:

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


// 創(chuàng)建服務(wù)器
http.createServer( function (request, response) {  
   // 解析請求,包括文件名
   var pathname = url.parse(request.url).pathname;
   
   // 輸出請求的文件名
   console.log("Request for " + pathname + " received.");
   
   // 從文件系統(tǒng)中讀取請求的文件內(nèi)容
   fs.readFile(pathname.substr(1), function (err, data) {
      if (err) {
         console.log(err);
         // HTTP 狀態(tài)碼: 404 : NOT FOUND
         // Content Type: text/plain
         response.writeHead(404, {'Content-Type': 'text/html'});
      }else{	         
         // HTTP 狀態(tài)碼: 200 : OK
         // Content Type: text/plain
         response.writeHead(200, {'Content-Type': 'text/html'});	
         
         // 響應(yīng)文件內(nèi)容
         response.write(data.toString());		
      }
      //  發(fā)送響應(yīng)數(shù)據(jù)
      response.end();
   });   
}).listen(8081);

// 控制臺(tái)會(huì)輸出以下信息
console.log('Server running at http://127.0.0.1:8081/');

接下來我們在該目錄下創(chuàng)建一個(gè)index.html文件,代碼如下:

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

執(zhí)行server.js文件:

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

接著我們在瀏覽器中輸入并打開地址:http://127.0.0.1:8081/index.html,顯示如下圖所示:

執(zhí)行server.js的控制臺(tái)輸出信息如下:

Server running at http://127.0.0.1:8081/
Request for /index.html received.     #  客戶端請求信息

Gif 實(shí)例演示

3

使用 Node 創(chuàng)建 Web 客戶端

使用Node創(chuàng)建Web客戶端需要引入http模塊,創(chuàng)建client.js文件,代碼如下所示:

var http = require('http');

// 用于請求的選項(xiàng)
var options = {
   host: 'localhost',
   port: '8081',
   path: '/index.html'  
};

// 處理響應(yīng)的回調(diào)函數(shù)
var callback = function(response){
   // 不斷更新數(shù)據(jù)
   var body = '';
   response.on('data', function(data) {
      body += data;
   });
   
   response.on('end', function() {
      // 數(shù)據(jù)接收完成
      console.log(body);
   });
}
// 向服務(wù)端發(fā)送請求
var req = http.request(options, callback);
req.end();

新開一個(gè)終端,執(zhí)行client.js文件,輸出結(jié)果如下:

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

執(zhí)行server.js的控制臺(tái)輸出信息如下:

Server running at http://127.0.0.1:8081/
Request for /index.html received.   # 客戶端請求信息