Modul Aliran Node.js

Modul Terpasang


Contoh

Tulis ke aliran yang dapat ditulisi:

var http = require('http');

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

Definisi dan Penggunaan

Modul Stream menyediakan cara untuk menangani data streaming.

Ada dua jenis aliran: dapat dibaca dan ditulis.

Contoh aliran yang dapat dibaca adalah objek respons yang Anda dapatkan saat bekerja dengan metode http.createServer().

Contoh aliran yang dapat ditulis adalah objek permintaan yang Anda dapatkan saat bekerja dengan metode http.createServer().


Sintaksis

Beberapa metode mengembalikan objek aliran yang dapat dibaca/ditulis, seperti http.createServer(), dan jika demikian, Anda tidak harus menyertakan modul aliran.

Jika tidak, sintaks untuk menyertakan modul Stream di aplikasi Anda:

var stream = require('stream');

Properti dan Metode Aliran yang Dapat Dibaca

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

Properti dan Metode Aliran yang Dapat Ditulis

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

Modul Terpasang