Node JS Stream Example

Node JS Stream is an interface. It has been implemented in a lots of node modules such as fs module, http module etc. This example will show you how to use node stream to read data from file, write data to file and use pipe stream to transfer data from one stream to another.

1. Node Stream Read Data From File.

// Require node fs module.
var file_system = require("fs");

// data variable is used to save read file data.
var data = '';

var file_path = '/Users/zhaosong/Documents/test.txt';

// Create file read stream.
var read_stream = file_system.createReadStream(file_path);

// Set read stream encoding to utf8.
read_stream.setEncoding('utf8');

// Process read stream events.
// Data reading event.
read_stream.on('data', function(chunk) {
    data += chunk;
});

// Read stream complete event.
read_stream.on('end',function(){
    // Output read data to console.
    console.log("Stream data is : " + data);
});

// Read stream error event.
read_stream.on('error', function(err){
    console.log(err.stack);
});

console.log("Read file stream complete");

Below is output.

Read file stream complete
Stream data is : This is a text file.

2. Node Stream Write Data To File.

// Require fs module.
var file_system = require("fs");

// Data which will be wrote to text file.
var data = 'dev2qa.com is a wonderful website.';

// Data will be wrote to below file.
var file_path = '/Users/zhaosong/Documents/output.txt';

// Create a write stream object.
var write_stream = file_system.createWriteStream(file_path);

// Write text with urf-8 character encoding.
write_stream.write(data,'utf-8');

// Write operation complete.
write_stream.end();

// Process write stream events.
// Process write finish event.
write_stream.on('finish', function() {
    console.log("Write success.");
});

// Process error event.
write_stream.on('error', function(err){
    console.log(err.stack);
});

console.log("Write stream to file complete.");

Below is output.

Write stream to file complete.
Write success.

node js write stream created file

3. Node JS Pipe Stream To Transfer Data Between Read Stream And Write Stream.

Pipe stream is used to transfer data from input stream to output stream.

node js pipe stream

// Require fs module.
var file_system = require("fs");

var source_file_path = '/Users/zhaosong/Documents/test.txt';

// Create a read stream.
var read_stream = file_system.createReadStream(source_file_path);

// Process read stream error event.
read_stream.on('error', function(error){
   console.log(error.stack);
});

// Create a write stream to overwrite the data in the target file.
var write_stream = file_system.createWriteStream('target.txt');

// Process write stream finish event.
write_stream.on('finish', function() {
    console.log("Write success.");
});

// Process write stream error event.
write_stream.on('error', function (error) {
   console.log(error.stack);
});

// If you want to append input stream data to the output stream
// then set 'flags' to 'a' when create the write stream.
//var write_stream = file_system.createWriteStream('target.txt', { 'flags': 'a' });

// Use pipe stream to read data from source file through input stream and
// write those data to destination file through write stream.
read_stream.pipe(write_stream);

console.log("Data transfer complete.");

Below is output.

Data transfer complete.
Write success.

You can find the file target.txt in current folder that execute the js code.

READ :   Implement Queue Using Node JS Async Module Example

4. Use Pipe Stream To Compress File To Zip Format.

node js zip unzip file diagram

This example will compress the target.txt file to zip format file target.txt.gz.

// Require fs module.
var file_system = require("fs");

// Require zlib module.
var zlib = require('zlib');

// Open read stream from source file.
var read_stream = file_system.createReadStream('target.txt');

// Create a zip write stream.
var zip_write_stream = zlib.createGzip();

// Create pipe stream between source file read stream and the zip write stream.
// This pipe stream will compress the source data to zip format.
var zip_pipe_stream = read_stream.pipe(zip_write_stream);

// Create a write stream to output zip file.
var write_stream = file_system.createWriteStream('target.txt.gz');

// Create a pipe stream between the zip_pipe_stream and write_stream
// to write compressed data to target zip file.
zip_pipe_stream.pipe(write_stream);

console.log("Compress zip file success.");

Below is the output.

Compress zip file success.

5. Use Pipe Stream To UnCompress Zip File.

// Require fs module.
var file_system = require("fs");

// Require zlib module.
var zlib = require('zlib');

// Create a read strem to read data from source zip file.
var zip_read_stream = file_system.createReadStream('target.txt.gz');

// Create a zip uncompress stream.
var unzip_stream = zlib.createGunzip();

// Create a pipe stream to connect zip file read stream and unzip stream.
var unzip_pipe_stream = zip_read_stream.pipe(unzip_stream);

// Create a target write stream that is used to write data to target file.
var target_write_stream = file_system.createWriteStream('target1.txt');

// Create a pipe stream to connect unzip pipe stream to target write stream.
unzip_pipe_stream.pipe(target_write_stream);

console.log("Unzip file success.");

Below is the output.

Unzip file success.
(Visited 48 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *