Streams are one of the fundamental concepts in Node.js. They allow you to process data piece by piece without loading everything into memory.
Types of Streams
- **Readable**: Sources of data (files, HTTP requests)
- **Writable**: Destinations for data (files, HTTP responses)
- **Duplex**: Both readable and writable (TCP sockets)
- **Transform**: Modify data as it passes through (compression)
Why Use Streams?
Streams are memory efficient and time efficient. Start processing data before it's fully available, and process files larger than your available memory.
Piping
The pipe method connects streams together. Data flows from readable to writable automatically with backpressure handling.
Stream Events
Streams emit events like 'data', 'end', 'error'. Understanding these is key to working with streams effectively.