Node.js- Stream - Modul
Beispiel
In einen beschreibbaren Stream schreiben:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Hello
World!');
res.end();
}).listen(8080);
Definition und Verwendung
Das Stream-Modul bietet eine Möglichkeit, Streaming-Daten zu verarbeiten.
Es gibt zwei Arten von Streams: lesbar und schreibbar.
Ein Beispiel für einen lesbaren Stream ist das Antwortobjekt , das Sie erhalten, wenn Sie mit der Methode http.createServer() arbeiten.
Ein Beispiel für einen beschreibbaren Stream ist das Anforderungsobjekt , das Sie erhalten, wenn Sie mit der Methode http.createServer() arbeiten.
Syntax
Einige Methoden geben ein lesbares/schreibbares Stream-Objekt zurück, wie http.createServer(), und wenn das der Fall ist, müssen Sie das Stream-Modul nicht einschließen.
Andernfalls die Syntax zum Einfügen des Stream-Moduls in Ihre Anwendung:
var stream = require('stream');
Lesbare Stream-Eigenschaften und -Methoden
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 |
Beschreibbare Stream-Eigenschaften und -Methoden
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 |