Understanding Readable Streams in NodeJS
What is a readable stream A readable stream is used for production data A stream for program consumption. Our common data production methods include reading disk files, reading network request content, etc. Let’s take a look at the example of stream usage introduced earlier: const rs = fs.createReadStream(filePath); rs is a readable stream, the way to produce data is to read disk files, our common console process.stdin is also a readable stream: process.stdin.pipe(process.stdout); You can print out the console input through a simple sentence, process.stdin The way to generate data is to read user input at the console. Look back at our definition of readable streams: readable streams are streams that produce data for program consumption. Custom readable stream In addition to the fs.CreateReadStream We also often use the src method provided by gulp or vinyl-fs gulp.src(['*.js', ' 39;dist/**/*.scss']) If we want to produce data in a specific way and hand it over to the program for consumption, how do we start? Just two simple steps Inherit the Readable class of the sream module Rewrite _read method, call this.push to put the produced data into the queue to be read The Readable class has completed most of the work to…
Understanding Readable Streams in NodeJS
What is a readable stream A readable stream is used for production data A stream for program consumption. Our common data production methods include reading disk files, reading network request content, etc. Let’s take a look at the example of stream usage introduced earlier: const rs = fs.createReadStream(filePath); rs is a readable stream, the way to produce data is to read disk files, our common console process.stdin is also a readable stream: process.stdin.pipe(process.stdout); You can print out the console input through a simple sentence, process.stdin The way to generate data is to read user input at the console. Look back at our definition of readable streams: readable streams are streams that produce data for program consumption. Custom readable stream In addition to the fs.CreateReadStream We also often use the src method provided by gulp or vinyl-fs gulp.src(['*.js', ' 39;dist/**/*.scss']) If we want to produce data in a specific way and hand it over to the program for consumption, how do we start? Just two simple steps Inherit the Readable class of the sream module Rewrite _read method, call this.push to put the produced data into the queue to be read The Readable class has completed most of the work to…