Tags
JS , NODE , stream
Asked 7 years ago
8 Feb 2017
Views 1547
samir

samir posted

how to use stream in Node JS ?

how to use stream in Node JS ?
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

simple streaming example in Node JS


var fs=require('fs');
var ws =fs.createWriteStream('sam.txt');
ws.write('blow mind \n');
ws.write('not it is not blowing my mind \n');
ws.end();


here we creating write stream for file . and writing write function

createWriteStream is the function which help to create stream.
and dont forget to close the stream with end function
Rasi

Rasi
answered Nov 30 '-1 00:00

filterstream.js

var Stream = require('stream');
var ts = new Stream;
ts.readable =true;
ts.writable = true;
ts.write = function (buff){
ts.emit('data',buff.toString().toUpperCase());

}
ts.end=function(buf){
  if(arguments.length){
  ts.write(buff);
}
ts.emit('end');

}
process.stdin.pipe(ts).pipe(process.stdout);


above is filter streaming example
var Stream = require('stream'); will load stream module
we making it readable and writable stream

ts.readable =true;
ts.writable = true;
Post Answer