Node.js - stroommodule

❮ Ingeboude modules


Voorbeeld

Skryf na 'n skryfbare stroom:

var http = require('http');

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

Definisie en gebruik

Die Stroom-module bied 'n manier om stroomdata te hanteer.

Daar is twee tipes strome: leesbaar en skryfbaar.

'n Voorbeeld van 'n leesbare stroom is die reaksie- objek wat jy kry wanneer jy met die http.createServer()-metode werk.

'n Voorbeeld van 'n skryfbare stroom is die versoekobjek wat jy kry wanneer jy met die http.createServer() metode werk.


Sintaksis

Sommige metodes gee 'n leesbare/skryfbare stroomvoorwerp terug, soos http.createServer(), en as dit die geval is, hoef jy nie die stroommodule in te sluit nie.

Andersins, die sintaksis vir die insluiting van die Stroom-module in jou toepassing:

var stream = require('stream');

Leesbare stroomeienskappe en -metodes

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

Skryfbare stroomeienskappe en -metodes

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

❮ Ingeboude modules