中文字幕一区二区人妻电影,亚洲av无码一区二区乱子伦as ,亚洲精品无码永久在线观看,亚洲成aⅴ人片久青草影院按摩,亚洲黑人巨大videos

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é)果返回給客戶端瀏覽器。

目前最主流的三個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');

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

實(shí)例

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/html response.writeHead(404, {'Content-Type': 'text/html'}); }else{ // HTTP 狀態(tài)碼: 200 : OK // Content Type: text/html response.writeHead(200, {'Content-Type': 'text/html'}); // 響應(yīng)文件內(nèi)容 response.write(data.toString()); } // 發(fā)送響應(yīng)數(shù)據(jù) response.end(); }); }).listen(8080); // 控制臺會輸出以下信息 console.log('Server running at http://127.0.0.1:8080/');

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

index.html 文件

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(json.cn)</title> </head> <body> <h1>我的第一個標(biāo)題</h1> <p>我的第一個段落。</p> </body> </html>

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

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

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

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

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

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

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

實(shí)例

var http = require('http'); // 用于請求的選項(xiàng) var options = { host: 'localhost', port: '8080', 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();

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

$ node  client.js 
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>小白教程(json.cn)</title>
</head>
<body>
    <h1>我的第一個標(biāo)題</h1>
    <p>我的第一個段落。</p>
</body>
</html>

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

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