Sobes.tech
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:

  1. Readable — streams from which data can be read (e.g., reading a file).
  2. Writable — streams to which data can be written (e.g., writing to a file).
  3. Duplex — streams that can both read and write data simultaneously (e.g., network sockets).
  4. 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);
});