Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a Writable stream in Node.js?
A Writable stream is an abstraction in Node.js that allows you to write data piece by piece to a destination, like a file or network socket, without loading everything into memory at once.
Click to reveal answer
beginner
How do you create a Writable stream to write to a file?
You use the fs module's createWriteStream method, like: <code>const fs = require('fs'); const stream = fs.createWriteStream('file.txt');</code> This stream lets you write data to 'file.txt' efficiently.
Click to reveal answer
beginner
What method do you use to write data to a Writable stream?
You use the write() method, passing the data you want to write. For example: stream.write('Hello'); writes 'Hello' to the stream.
Click to reveal answer
intermediate
Why should you listen for the 'drain' event when writing to a Writable stream?
Because write() returns false if the internal buffer is full. The 'drain' event tells you when it's safe to write more data without overloading memory.
Click to reveal answer
beginner
How do you properly close a Writable stream after writing data?
You call the end() method on the stream. This signals that no more data will be written and lets the stream finish writing any buffered data.
Click to reveal answer
Which method starts writing data to a Writable stream in Node.js?
Apipe()
Bread()
Cwrite()
Dopen()
✗ Incorrect
The write() method is used to send data to a Writable stream.
What does the write() method return if the internal buffer is full?
Atrue
Bundefined
Cnull
Dfalse
✗ Incorrect
write() returns false when the buffer is full, signaling you should wait before writing more.
Which event should you listen to before resuming writing after a false return from write()?
Adrain
Berror
Cfinish
Dclose
✗ Incorrect
The 'drain' event indicates the buffer has emptied and you can write again.
How do you signal that you have finished writing data to a Writable stream?
Aclose()
Bend()
Cfinish()
Ddestroy()
✗ Incorrect
Calling end() tells the stream no more data will be written.
Which Node.js module provides the createWriteStream method?
Afs
Bstream
Cnet
Dhttp
✗ Incorrect
The fs module provides createWriteStream to write files.
Explain how to write data to a file using a Writable stream in Node.js.
Think about creating the stream, writing data, handling buffer limits, and closing.
You got /5 concepts.
Describe why handling the 'drain' event is important when writing large amounts of data.
Consider what happens when the stream's internal buffer fills up.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a Writable stream in Node.js?
easy
A. To send data piece by piece to a destination
B. To read data from a file
C. To create a server
D. To handle HTTP requests
Solution
Step 1: Understand Writable stream role
Writable streams are designed to send data to a destination in chunks.
Step 2: Compare with other options
Reading data is done by Readable streams, not Writable. Creating servers and handling HTTP requests are unrelated to Writable streams.
Final Answer:
To send data piece by piece to a destination -> Option A
Quick Check:
Writable stream = send data [OK]
Hint: Writable streams send data out chunk by chunk [OK]
Common Mistakes:
Confusing Writable with Readable streams
Thinking Writable streams read data
Mixing streams with server creation
2. Which of the following is the correct way to implement the _write method in a custom Writable stream?
easy
A. _write(chunk, encoding, callback) { callback(); }
B. _write(chunk, encoding) { return chunk; }
C. _write(chunk) { console.log(chunk); }
D. _write() { return true; }
Solution
Step 1: Recall _write method signature
The _write method must accept three parameters: chunk, encoding, and callback.
Step 2: Check callback usage
Calling callback() signals that the chunk was processed. Omitting it causes the stream to hang.
Final Answer:
_write(chunk, encoding, callback) { callback(); } -> Option A
Quick Check:
_write needs callback() [OK]
Hint: Always include callback in _write and call it [OK]
B. Missing callback parameter and not calling callback()
C. Not calling stream.end() causes error
D. _write method should be named write
Solution
Step 1: Check _write method signature
_write must have three parameters: chunk, encoding, callback.
Step 2: Check callback usage
Callback must be called to signal completion; missing callback causes stream to hang.
Final Answer:
Missing callback parameter and not calling callback() -> Option B
Quick Check:
_write needs callback param and call [OK]
Hint: Always include and call callback in _write [OK]
Common Mistakes:
Omitting callback parameter
Not calling callback inside _write
Confusing _write with write method
5. You want to create a Writable stream that collects all written chunks into a single string and logs it only when the stream ends. Which approach is correct?
hard
A. Log each chunk inside _write and ignore 'finish' event
B. Call callback only after all chunks are written, ignoring _write
C. Store chunks in a variable inside _write, call callback, then log in 'finish' event
D. Use readable stream instead of writable for collecting data
Solution
Step 1: Collect chunks inside _write
Inside _write, append each chunk to a variable and call callback to continue.
Step 2: Log combined data on 'finish' event
Listen to the 'finish' event to know when writing ends, then log the full collected string.
Final Answer:
Store chunks in a variable inside _write, call callback, then log in 'finish' event -> Option C
Quick Check:
Collect chunks + log on finish = Store chunks in a variable inside _write, call callback, then log in 'finish' event [OK]
Hint: Collect chunks in _write, log on 'finish' event [OK]