
- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境搭建
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送電子郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTful API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 從表中選擇資料
- Node.js - MySQL Where 條件
- Node.js - MySQL Order By 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 聯接
- Node.js MongoDB
- Node.js - MongoDB 入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 限制資料
- Node.js - MongoDB 聯接
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
Node.js - Web 模組
什麼是 Web 伺服器?
Node.js 中的 http 模組支援伺服器和客戶端之間透過超文字傳輸協議 (HTTP) 傳輸資料。http 模組中的 createServer() 函式建立 Node.js http 伺服器的例項。它監聽來自其他 http 客戶端在指定主機和埠上的傳入請求。
Node.js 伺服器是一個軟體應用程式,它處理由 HTTP 客戶端(如 Web 瀏覽器)傳送的 HTTP 請求,並響應客戶端的請求返回網頁。Web 伺服器通常提供 html 文件以及影像、樣式表和指令碼。
大多數 Web 伺服器都支援伺服器端指令碼,使用指令碼語言或將任務重定向到應用程式伺服器,應用程式伺服器從資料庫中檢索資料並執行復雜的邏輯,然後透過 Web 伺服器將結果傳送到 HTTP 客戶端。
Web 應用程式架構
Web 應用程式通常分為四個層:

客戶端 - 此層包含 Web 瀏覽器、移動瀏覽器或可以向 Web 伺服器發出 HTTP 請求的應用程式。
伺服器 - 此層包含 Web 伺服器,它可以攔截客戶端發出的請求並傳遞響應。
業務邏輯 - 此層包含應用程式伺服器,Web 伺服器利用它來執行所需的處理。此層透過資料庫或某些外部程式與資料層互動。
資料 - 此層包含資料庫或任何其他資料來源。
使用 Node 建立 Web 伺服器
Node.js 提供了一個 http 模組,可用於建立伺服器的 HTTP 客戶端。以下是監聽 5000 埠的 HTTP 伺服器的最小結構。
建立一個名為 server.js 的 js 檔案:
var http = require('http'); var fs = require('fs'); var url = require('url'); // Create a server http.createServer( function (request, response) { // Parse the request containing file name var pathname = url.parse(request.url).pathname; // Print the name of the file for which request is made. console.log("Request for " + pathname + " received."); // Read the requested file content from file system fs.readFile(pathname.substr(1), function (err, data) { if (err) { console.log(err); } else { response.writeHead(200, {'Content-Type': 'text/html'}); // Write the content of the file to response body console.log(data.toString()); response.write(data.toString()); } // Send the response body response.end(); }); }).listen(5000); // Console will print the message console.log('Server running at http://127.0.0.1:5000/');
createServer() 函式開始監聽 localhost 的 5000 埠上的客戶端請求。Http 請求和伺服器響應物件由 Nde.js 伺服器內部提供。它獲取 HTTP 客戶端請求的 HTML 檔案的 URL。createServer() 函式的回撥函式讀取請求的檔案,並將它的內容作為伺服器的響應寫入。
您可以在伺服器機器本身的瀏覽器上傳送 HTTP 請求。執行上述 server.js 檔案,並在瀏覽器視窗中輸入 https://:5000/index.html 作為 URL。index.html 頁面的內容將被呈現。
傳送對任何其他現有網頁的請求,例如 https://:5000/hello.html,請求的頁面將被呈現。
使用 Node 建立 Web 客戶端
可以使用 http 模組建立 Web 客戶端。讓我們檢查以下示例。
建立一個名為 client.js 的 js 檔案(將此檔案儲存在另一個資料夾中,不要儲存在包含 server.js 指令碼的資料夾中):
var http = require('http'); var fs = require('fs'); var path = require('path'); // Options to be used by request var options = { host: 'localhost', port: '5000', path: path.join('/',process.argv[2]) }; var body = ''; // Callback function is used to deal with response var callback = function(response) { // Continuously update stream with data response.on('data', function(data) { body += data; }); response.on('end', function() { // Data received completely. console.log(body); fs.writeFile(options.path.substr(1), body, function (err) { if (err) console.log(err); else console.log('Write operation complete.'); }); }); } // Make a request to the server var req = http.request(options, callback); req.end();
此客戶端指令碼傳送對作為命令列引數給出的網頁的 HTTP 請求。例如:
node main.js index.html
檔名稱是引數列表中的 argv[2]。它用作要傳遞給 http.request() 方法的 options 引數中的路徑引數。一旦伺服器接受此請求,它的內容就會寫入響應流。client.js 接收此響應並將接收到的資料作為新檔案寫入客戶端的資料夾。
首先執行伺服器程式碼。從另一個終端執行客戶端程式碼。您將看到在客戶端資料夾中建立的請求的 HTML 檔案。