Junior — Middle
Can you list the main types of data streams?
sobes.tech AI
Answer from AI
In Node.js, the main types of data streams are divided into four categories:
- Readable — streams from which data can be read (e.g., reading a file).
- Writable — streams to which data can be written (e.g., writing to a file).
- Duplex — streams that can both read and write data simultaneously (e.g., network sockets).
- Transform — special duplex streams that can modify data during reading or writing (e.g., data compression or encryption).
Example of creating a readable stream:
const fs = require('fs');
const readable = fs.createReadStream('file.txt');
readable.on('data', chunk => {
console.log('Received chunk:', chunk);
});