Node.js is a platform built on the Chrome JavaScript runtime.
Node.js is an event-driven I/O server-side JavaScript environment based on Google's V8 engine. The V8 engine executes Javascript very quickly and has very good performance.
Node.js Stream(stream) syntax
Stream is an abstract interface, and many objects in Node implement this interface. For example, the request object that initiates a request to the http server is a Stream, as well as stdout (standard output).
Node.js, Stream has four stream types:
Readable - readable operation.
Writable - Writable operations.
Duplex - Read and write operations.
Transform - The operation writes data and then reads the result.
All Stream objects are instances of EventEmitter. Commonly used events are:
data - triggered when data is readable.
end - Fired when there is no more data to read.
error - triggered when an error occurs during reception and writing.
finish - Triggered when all data has been written to the underlying system.
Node.js Stream(stream) example
Create the main.js file with the following code:
var fs = require("fs");var data = ''; // 创建可读流 var readerStream = fs.createReadStream('input.txt'); // 设置编码为 utf8。 readerStream.setEncoding('UTF8'); // 处理流事件 --> 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("程序执行完毕");