Nodejs and Streams detailed review?

Can someone explain to us (just me?) How to use Streams in Nodejs?

This Continuation: Compressing and decompressing data with zlib in Nodejs

And my main interest will be working with files, but also with strings (i.e. Stream.toString () and String.toStream () ... not a real function ...)

Thanks!

+4
source share
1 answer

A stream is an abstract interface implemented by various objects in Node. For example, a request to an HTTP server is a stream, like stdout. Streams are read, write, or both. All threads are instances of EventEmitter. ( Flow documentation )

This means that Stream is a useful object used by several Node kernel objects to read and / or write information. All major objects use this to improve the way information is transferred from one object to another. Since Stream is an instance of EventEmitter, your code may be asynchronous and may not stop when reading information from somewhere.

// imagine 'response' is the output Stream from a client connection var video = fs.createReadStream("/path/to/video.mpg"); // pipe video to response (while data is being read asynchronously) video.pipe(response); 

Check stream.pipe .

For example, to stream a video to an HTTP client when reading it from a file. Or a download stream to a local file. Use your imagination.

+4
source

All Articles