0
0
Node.jsframework~30 mins

Stream types (Readable, Writable, Transform, Duplex) in Node.js - Mini Project: Build & Apply

Choose your learning style9 modes available
Node.js Stream Types: Readable, Writable, Transform, Duplex
📖 Scenario: You are building a simple Node.js program that demonstrates how different stream types work. Streams help handle data piece by piece, like reading a book page by page instead of all at once.This project will show how to create and use four stream types: Readable, Writable, Transform, and Duplex.
🎯 Goal: Build a Node.js script that creates one stream of each type:A Readable stream that emits three lines of text.A Writable stream that collects and stores data it receives.A Transform stream that changes all text to uppercase.A Duplex stream that can both read and write data, here it will reverse the text.Connect these streams to see how data flows and changes.
📋 What You'll Learn
Create a Readable stream emitting exactly these strings: 'Hello', 'Node.js', 'Streams'
Create a Writable stream that stores received data in an array called collectedData
Create a Transform stream that converts input text to uppercase
Create a Duplex stream that reverses the text it receives and outputs it
Pipe the Readable stream through the Transform stream, then into the Writable stream
Demonstrate writing and reading from the Duplex stream
💡 Why This Matters
🌍 Real World
Streams are used in Node.js to efficiently handle large files, network data, or any data that comes in chunks, like video streaming or reading big logs.
💼 Career
Understanding streams is essential for backend developers working with Node.js, especially for building fast, memory-efficient applications that process data continuously.
Progress0 / 4 steps
1
Create a Readable stream emitting fixed text
Write code to create a Readable stream called myReadable using Node.js stream module's Readable class. It should emit exactly these strings in order: 'Hello', 'Node.js', and 'Streams'. Use the read() method to push these strings and then push null to signal the end.
Node.js
Need a hint?

Use new Readable({ read() { ... } }) and inside read() push the strings one by one, then push null.

2
Create a Writable stream to collect data
Add code to create a Writable stream called myWritable using Node.js Writable class. It should collect all data chunks it receives into an array named collectedData. Initialize collectedData as an empty array before creating the stream. Implement the write(chunk, encoding, callback) method to push each chunk converted to string into collectedData and call callback().
Node.js
Need a hint?

Initialize collectedData as an empty array. Use new Writable({ write(chunk, encoding, callback) { ... } }) and push each chunk as string into collectedData.

3
Create a Transform stream to uppercase text
Add code to create a Transform stream called myTransform using Node.js Transform class. Implement the _transform(chunk, encoding, callback) method to convert the chunk to uppercase string and push it forward. Call callback() after pushing.
Node.js
Need a hint?

Use new Transform({ _transform(chunk, encoding, callback) { ... } }). Convert chunk to uppercase string and push it.

4
Create a Duplex stream to reverse text and connect streams
Add code to create a Duplex stream called myDuplex using Node.js Duplex class. Implement _write(chunk, encoding, callback) to store the chunk string reversed in a variable lastData and call callback(). Implement _read() to push lastData and then push null. Then pipe myReadable through myTransform into myWritable. Finally, write the string 'Node' to myDuplex and read from it by attaching a data event listener that pushes received data into an array duplexOutput.
Node.js
Need a hint?

Use new Duplex({ _write(chunk, encoding, callback) { ... }, _read() { ... } }). Reverse the string in _write and push it in _read. Pipe the streams as instructed. Write and read from myDuplex and collect data in duplexOutput.