Node.js Stream Module
THE WORLD'S LARGEST WEB DEVELOPER SITE

Node.js Stream Module

❮ Built-in Modules


Example

Write to a writeable stream:

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write('Hello World!');
  res.end();
}).listen(8080);
Run example »

Definition and Usage

The Stream module provides a way of handling streaming data.

There are two types of streams: readable and writeable.

An example of a readable stream is the response object you get when working with the http.createServer() method.

An example of a writable stream is the request object you get when working with the http.createServer() method.


Syntax

Some methods returns a readable/writable stream object, like http.createServer(), and if that is the case, you do not have to include the stream module.

Otherwise, the syntax for including the Stream module in your application:

var stream = require('stream');

Readable Stream Properties and Methods

Method Description
isPaused() Returns true if the state of  the readable stream is paused, otherwise false
pause() Pauses the readable stream
pipe() Turns the readable stream into the specified writable stream
read() Returns a specified part of the readable stream
resume() Resumes a paused stream
setEncoding() Sets the character encoding of the readable stream
unpipe() Stops turning a readable stream into a writable stream, caused by the pipe() method
unshift() Pushes some specified data back into the internal buffer
wrap() Helps reading streams made by older Node.js versions

Writable Stream Properties and Methods

Method Description
cork() Stops the writable stream and all written data will be buffered in memory
end() Ends the writable stream
setDefaultEncoding() Sets the encoding for the writable stream
uncork() Flushes all data that has been buffered since the cork() method was called
write() Writes data to the stream

❮ Built-in Modules