
- 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 排序
- 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 - 流
什麼是流?
流是資料的集合。但是,與陣列或字串不同,流物件中的所有資料不會一次性儲存在記憶體中。相反,一次只將流中的單個數據塊載入到記憶體中。這使得流更加高效。Node.js 應用非常適合開發資料流應用。
流的型別
Node.js 處理四種基本的流型別:
可寫流 - 可以寫入資料的流。
可讀流 - 可以從中讀取資料的流。
雙工流 - 既是可讀流又是可寫流的流。
轉換流 - 可以在寫入和讀取資料時修改或轉換資料的雙工流。
每種型別的流都是 EventEmitter 例項,並在不同的時間例項中觸發多個事件。例如,一些常用的事件包括:
data - 當有資料可供讀取時觸發此事件。
end - 當沒有更多資料可供讀取時觸發此事件。
error - 當接收或寫入資料時發生任何錯誤時觸發此事件。
finish - 當所有資料都已重新整理到底層系統時觸發此事件。
本章中的示例使用名為 input.text 的檔案,其中包含以下資料。
Tutorials Point is giving self learning content to teach the world in simple and easy way!!!!!
可讀流
檔案物件充當一個流,可以從中以特定大小的塊讀取資料。在下面的示例中,我們從 fs 模組呼叫 createReadStream() 函式以從給定檔案讀取資料。可讀流的 on 事件收集檔案內容,直到觸發 end 事件。
var fs = require("fs"); var data = ''; // Create a readable stream var readerStream = fs.createReadStream('input.txt'); // Set the encoding to be utf8. readerStream.setEncoding('UTF8'); // Handle stream events --> data, end, and error readerStream.on('data', function(chunk) { data += chunk; }); readerStream.on('end',function() { console.log(data); }); readerStream.on('error', function(err) { console.log(err.stack); }); console.log("Program Ended");
將上述指令碼儲存為 main.js。執行上述 Node.js 應用。它將顯示 input.text 檔案的內容。
可寫流
fs 模組中的 createWriteStream() 函式建立一個可寫流物件。它的 write() 方法將資料儲存在作為引數傳遞給 createWriteStream() 函式的檔案中。
將以下程式碼儲存為名為 main.js 的檔案。
var fs = require("fs"); var data = `Tutorials Point is giving self learning content to teach the world in simple and easy way!!!!!`; // Create a writable stream var writerStream = fs.createWriteStream('output.txt'); // Write the data to stream with encoding to be utf8 writerStream.write(data,'UTF8'); // Mark the end of file writerStream.end(); // Handle stream events --> finish, and error writerStream.on('finish', function() { console.log("Write completed."); }); writerStream.on('error', function(err){ console.log(err.stack); }); console.log("Program Ended");
現在開啟在當前目錄中建立的 output.txt 檔案,以檢查它是否包含給定的資料。
管道流
管道是一種機制,我們將其提供一個流的輸出作為另一個流的輸入。它通常用於從一個流獲取資料並將該流的輸出傳遞給另一個流。管道操作沒有限制。現在我們將展示一個從一個檔案讀取並將其寫入另一個檔案的管道示例。
建立一個名為 main.js 的 js 檔案,其中包含以下程式碼:
var fs = require("fs"); // Create a readable stream var readerStream = fs.createReadStream('input.txt'); // Create a writable stream var writerStream = fs.createWriteStream('output.txt'); // Pipe the read and write operations // read input.txt and write data to output.txt readerStream.pipe(writerStream); console.log("Program Ended");
現在執行 main.js 以檢視結果:
node main.js
驗證輸出。
Program Ended
開啟在當前目錄中建立的 output.txt 檔案;它應該包含以下內容:
Tutorials Point is giving self learning content to teach the world in simple and easy way!!!!!
連結流
連結是一種將一個流的輸出連線到另一個流並建立多個流操作鏈的機制。它通常與管道操作一起使用。現在我們將使用管道和連結首先壓縮一個檔案,然後解壓縮同一個檔案。
建立一個名為 main.js 的 js 檔案,其中包含以下程式碼:
var fs = require("fs"); var zlib = require('zlib'); // Compress the file input.txt to input.txt.gz fs.createReadStream('input.txt') .pipe(zlib.createGzip()) .pipe(fs.createWriteStream('input.txt.gz')); console.log("File Compressed.");
現在執行 main.js 以檢視結果:
node main.js
驗證輸出。
File Compressed.
您會發現 input.txt 已被壓縮,並在當前目錄中建立了一個名為 input.txt.gz 的檔案。現在讓我們嘗試使用以下程式碼解壓縮同一個檔案:
var fs = require("fs"); var zlib = require('zlib'); // Decompress the file input.txt.gz to input.txt fs.createReadStream('input.txt.gz') .pipe(zlib.createGunzip()) .pipe(fs.createWriteStream('input.txt')); console.log("File Decompressed.");
現在執行 main.js 以檢視結果:
node main.js
驗證輸出。
File Decompressed.