Search This Blog

2015-12-05

Node JS Streams

A stream is an abstract interface implemented by various objects in Node.js. For example a request to an HTTP server is a stream. Streams are readable, writable, or both. All streams are instances of EventEmitter

You can load the Stream base classes by doing require('stream'). There are base classes provided for Readable streams, Writable streams, Duplex streams(both readable and writable), and Transform streams(Transform streams are Duplex streams where the output is in some way computed from the input).

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 data has been flushed to underlying system
Example:

Step 1: Run "npm init"

Step 2 : Insert all required parameter in 'npm init' and create the start page 'index.js'.

Step 3: index.js will be as below

var fs = require("fs");
var data = '';


// Create a readable stream
var readerStream = fs.createReadStream('myinputfile.txt');

// Set the encoding to be utf8.
readerStream.setEncoding('UTF8');

// Handle stream events --> data, end, and error
readerStream.on('data', function(chunk) {//The data event is fired when there is data is available to read.
   console.log('data event from Read Stream');
   data += chunk;
});

readerStream.on('end',function(){//The end event is fired when there is no more data to read.

   console.log('end event from Read Stream: ' + data);
});

readerStream.on('finish',function(){//The finish event is fired when all data has been flushed to underlying system
   console.log('finish event from Read Stream: ' +data);
});

readerStream.on('error', function(err){//The Error event is fired when there is any error receiving or writing data.
   console.log(err.stack);
});

console.log("Program Ended for Read Stream");
///////---------------------------------Write Stream--------------------------------------
console.log(data);
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 from write Stream.");
});

writerStream.on('error', function(err){
   console.log(err.stack);
});

console.log("Program Ended for write stream");

Step 4: Create a new file 'myinputfile.txt' within the root folder with the text  'Hi, This is Manab'

Step 5:Run the application by 'node index.js'

Step 6: You could see a new file 'output.txt' has been generated within the root and below is the output in command prompt

No comments: