Módulo de transmisión de Node.js
Ejemplo
Escribir en una secuencia grabable:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Hello
World!');
res.end();
}).listen(8080);
Definición y uso
El módulo Stream proporciona una forma de manejar datos de transmisión.
Hay dos tipos de flujos: legibles y escribibles.
Un ejemplo de flujo legible es el objeto de respuesta que obtienes cuando trabajas con el método http.createServer().
Un ejemplo de flujo de escritura es el objeto de solicitud que obtienes cuando trabajas con el método http.createServer().
Sintaxis
Algunos métodos devuelven un objeto de flujo legible/escribible, como http.createServer(), y si ese es el caso, no es necesario que incluya el módulo de flujo.
De lo contrario, la sintaxis para incluir el módulo Stream en su aplicación:
var stream = require('stream');
Propiedades y métodos de transmisión legible
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 |
Propiedades y métodos de flujo de escritura
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 |