Módulo de fluxo Node.js
Exemplo
Gravar em um fluxo gravável:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Hello
World!');
res.end();
}).listen(8080);
Definição e uso
O módulo Stream fornece uma maneira de lidar com dados de streaming.
Existem dois tipos de fluxos: legíveis e graváveis.
Um exemplo de fluxo legível é o objeto de resposta que você obtém ao trabalhar com o método http.createServer().
Um exemplo de fluxo gravável é o objeto de solicitação que você obtém ao trabalhar com o método http.createServer().
Sintaxe
Alguns métodos retornam um objeto stream legível/gravável, como http.createServer(), e se for o caso, você não precisa incluir o módulo stream.
Caso contrário, a sintaxe para incluir o módulo Stream em seu aplicativo:
var stream = require('stream');
Propriedades e métodos de fluxo legíveis
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 |
Propriedades e métodos de fluxo gravável
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 |