node js - Node js Streams- node - node js tutorial - webnode




What is Streams in Node.js?

  • Streams are the objects that facilitate you to read data from a source and write data to a destination. There are four types of streams in Node.js:
    • Readable − Stream which is used for read operation.
    • Writable − Stream which is used for write operation.
    • Duplex − Stream which can be used for both read and write operation.
    • Transform − A type of duplex stream where the output is computed based on input.
learn nodejs - nodejs tutorial - nodejs streams - nodejs example programs
 process of node.js stream
  • Each type of Stream is an EventEmitter instance and throws several events at different instance of times. For example, some of the commonly used events are
    • data − This event is fired when there is data is available to read.
    • end − This event is fired when there is no more data to read.
    • error − This event is fired when there is any error receiving or writing data.
    • finish − This event is fired when all the data has been flushed to underlying system.
 nodejs streams

Learn Node js - node js Tutorial - Node.js streams - node - Node js Examples

Reading from a Stream

  • Create a text file named input.txt having the following content
Wikitechy is giving self learning content
to learn different technologies in easy and effective manner!!!!!
  • Create a js file named main.js with the following code

main.js

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");
  • Now run the main.js to see the result
$ node main.js

Output.

Program Ended
Wikitechy is giving self learning content
to learn different technologies in easy and effective manner!!!!!
Related nodejs article tags - node js - node js tutorial - node js examples

Writing to a Stream

  • Create a js file named main.js with the following code

main.js

var fs = require("fs");
var data = 'Simply Easy Learning';
// 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");
  • Now run the main.js to see the result
$ node main.js

Output

Program Ended
Write completed.
  • Now open output.txt created in your current directory; it should contain the following
Simply Easy Learning

Piping the Streams

  • Piping is a mechanism where we provide the output of one stream as the input to another stream.
  • It is normally used to get data from one stream and to pass the output of that stream to another stream.
  • There is no limit on piping operations.

Piping example for reading from one file and writing it to another file.Create a js file named main.js with the following code

main.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");
  • Now run the main.js to see the result
$ node main.js

Output

Program Ended
  • Open output.txt created in your current directory; it should contain the following
Wikitechy is giving self learning content
to learn different technologies in easy and effective manner!!!!!

Chaining the Streams

  • Chaining is a mechanism to connect the output of one stream to another stream and create a chain of multiple stream operations.
  • It is normally used with piping operations.

Now we'll use piping and chaining to first compress a file and then decompress the same.Create a js file named main.js with the following code

main.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.");
  • Now run the main.js to see the result
$ node main.js

Node JS Output :

File Compressed.
  • You will find that input.txt has been compressed and it created a file input.txt.gz in the current directory.
  • To decompress the same file using the following code
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.");
  • Now run the main.js to see the result
$ node main.js

Node JS Output :

File Decompressed.

This wikitechy technological portal provides you whole lot of information related to the topics such as mongodb tutorial , what is node js , express js tutorial , node js tutorial pdf , learn node js , node js tutorial for beginners , node js tutorial w3schools , node js express , javascript for beginners , node js express tutorial , node js tutorial point , node js book , node js server , note js , node js mongodb , what is node js used for , why node js , node tutorial , node js basics , tutorialspoint node js , node express , node js sample application , nodeschool , node js for beginners , express tutorial , node js application , use of node js , npm tutorial , node js org , node js training , node js online training , node js tutorial beginner , how to use node js , node js hello world , express node js , node js mongodb tutorial , nodejs http , why use node js

Related Searches to Node.js Streams

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×