亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Node.js 中文參考手冊(cè) / Node.js Web 模塊

Node.js Web 模塊

Node.js Web 模塊


什么是 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ù)器請(qǐng)求數(shù)據(jù)。

  • Server - 服務(wù)端,一般指 Web 服務(wù)器,可以接收客戶端請(qǐng)求,并向客戶端發(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) {  
   // 解析請(qǐng)求,包括文件名   var pathname = url.parse(request.url).pathname;   
   // 輸出請(qǐng)求的文件名
   console.log("Request for " + pathname + " received.");   
   // 從文件系統(tǒng)中讀取請(qǐ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/');

接下來我們?cè)谠撃夸浵聞?chuàng)建一個(gè) index.htm 文件,代碼如下:

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

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

$ node server.jsServer running at http://127.0.0.1:8081/

接著我們?cè)跒g覽器中打開地址:http://127.0.0.1:8081/index.htm,顯示如下圖所示:

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

Server running at http://127.0.0.1:8081/Request for /index.htm received.     #  客戶端請(qǐng)求信息

Gif 實(shí)例演示


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

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

var http = require('http');// 用于請(qǐng)求的選項(xiàng)var options = {
   host: 'localhost',
   port: '8081',
   path: '/index.htm'  };// 處理響應(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ā)送請(qǐng)求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.htm received.   # 客戶端請(qǐng)求信息

Gif 實(shí)例演示