i

Streams

A Stream is a built-in module for working with data streaming in Node.js. There are many stream objects created in Node.js. Streams can be readable, writable or both.

To access the stream module

const stream = require(‘stream’);

There are four types of stream types within Node.js.

1. Writable

2. Readable

3. Duplex

4. Transform

1. Writable : Streams to which data can be written

e.g. : fs.createWriteStream()

Writable streams will store data in an internal buffer.

2. Readable : Stream from which data can be read.

e.g : fs.createReadStream()

Also, Readable streams will store data in an internal buffer. The amount of data buffered depends on the high watermark.

3. Duplex : Stream can be readable as well as writable.

e.g : net.socket()

4. Transform : Duplex streams that can modify or transform as it is written an read.

e.g. : zlib.createDeflate().

Both duplex and Transform streams are both readable and writable. Both duplex and Transform maintain two separate internal buffers for reading and writing.

Net.socket are duplex streams.

Each type of Stream is an EventEmitter instance and throws several events at a different instance of times like data, end, error, and finish.

Stream-demo.js

Running stream-demo.js

Using nodemon stream-demo.js

On running URL from browser:

On running the server from the browser, we receive the contents of form.html