Bird
Raised Fist0
Node.jsframework~10 mins

Why streams are needed in Node.js - Test Your Understanding

Choose your learning style10 modes available

Start learning this pattern below

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a readable stream from a file.

Node.js
const fs = require('fs');
const readable = fs.createReadStream([1]);
Drag options to blanks, or click blank then click option'
A'input.txt'
B'write.txt'
C'output.txt'
D'config.json'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a file name meant for writing instead of reading.
2fill in blank
medium

Complete the code to pipe data from a readable stream to a writable stream.

Node.js
readableStream.[1](writableStream);
Drag options to blanks, or click blank then click option'
Awrite
Bpipe
Csend
Dtransfer
Attempts:
3 left
💡 Hint
Common Mistakes
Using methods that don't exist on streams like 'write' or 'send'.
3fill in blank
hard

Fix the error in the code to handle stream errors properly.

Node.js
readableStream.on('error', [1]);
Drag options to blanks, or click blank then click option'
Aconsole.info
Bconsole.warn
Cconsole.log
Dconsole.error
Attempts:
3 left
💡 Hint
Common Mistakes
Using console.log which is less clear for errors.
4fill in blank
hard

Fill both blanks to create a writable stream and write data to it.

Node.js
const writable = fs.createWriteStream([1]);
writable.[2]('Hello, streams!');
Drag options to blanks, or click blank then click option'
A'output.txt'
Bwrite
Cend
D'input.txt'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the input file name for writing or using the wrong method like 'end' to write data.
5fill in blank
hard

Fill all three blanks to create a transform stream that modifies data.

Node.js
const { Transform } = require('stream');
const upperCaseTransform = new Transform({
  transform(chunk, encoding, callback) {
    this.push(chunk.toString().[1]());
    callback();
  }
});
readableStream.[2](upperCaseTransform).[3](writableStream);
Drag options to blanks, or click blank then click option'
AtoUpperCase
Bpipe
DtoLowerCase
Attempts:
3 left
💡 Hint
Common Mistakes
Using toLowerCase instead of toUpperCase or missing pipe connections.

Practice

(1/5)
1. Why are streams needed in Node.js when working with large files?
easy
A. To process data piece by piece without loading the entire file into memory
B. To make the file smaller in size automatically
C. To convert files into images
D. To encrypt the file contents

Solution

  1. Step 1: Understand memory usage with large files

    Loading a large file fully into memory can cause high memory use or crashes.
  2. Step 2: Role of streams in data processing

    Streams let you read or write data in small chunks, reducing memory needs.
  3. Final Answer:

    To process data piece by piece without loading the entire file into memory -> Option A
  4. Quick Check:

    Streams save memory by chunking data [OK]
Hint: Streams handle data in chunks, not all at once [OK]
Common Mistakes:
  • Thinking streams reduce file size
  • Confusing streams with encryption
  • Assuming streams convert file types
2. Which of the following is the correct way to create a readable stream from a file in Node.js?
easy
A. const stream = fs.readFile('file.txt');
B. const stream = fs.createWriteStream('file.txt');
C. const stream = fs.createReadStream('file.txt');
D. const stream = fs.open('file.txt');

Solution

  1. Step 1: Identify the method for readable streams

    Node.js uses fs.createReadStream() to read files as streams.
  2. Step 2: Check other options

    fs.createWriteStream() is for writing, fs.readFile() reads whole file at once, fs.open() opens file descriptor.
  3. Final Answer:

    const stream = fs.createReadStream('file.txt'); -> Option C
  4. Quick Check:

    Read streams use createReadStream() [OK]
Hint: Read streams use createReadStream(), write streams use createWriteStream() [OK]
Common Mistakes:
  • Using createWriteStream for reading
  • Using readFile which reads whole file at once
  • Confusing open() with stream creation
3. What will the following code output when reading a large file using streams?
const fs = require('fs');
const stream = fs.createReadStream('largefile.txt');
stream.on('data', chunk => {
  console.log(chunk.length);
});
medium
A. Multiple numbers showing sizes of each chunk read
B. The total size of the file in bytes printed once
C. An error message because chunk.length is invalid
D. Nothing, because streams do not emit data events

Solution

  1. Step 1: Understand 'data' event on readable streams

    The 'data' event fires multiple times, each with a chunk of data.
  2. Step 2: What does chunk.length represent?

    chunk.length gives the size of each chunk in bytes, so multiple numbers print.
  3. Final Answer:

    Multiple numbers showing sizes of each chunk read -> Option A
  4. Quick Check:

    'data' event outputs chunk sizes repeatedly [OK]
Hint: Streams emit 'data' events repeatedly with chunks [OK]
Common Mistakes:
  • Expecting one total size output
  • Thinking chunk.length is undefined
  • Believing streams don't emit 'data'
4. Identify the error in this code snippet that tries to read a file using streams:
const fs = require('fs');
const stream = fs.createReadStream('file.txt');
stream.on('data', (chunk) => {
  console.log(chunk.toString);
});
medium
A. Not handling 'end' event to close the stream
B. Using createReadStream instead of createWriteStream
C. Using arrow function incorrectly
D. Missing parentheses after toString method call

Solution

  1. Step 1: Check usage of toString method

    toString is a method and needs parentheses to execute: toString()
  2. Step 2: Verify other parts of code

    createReadStream is correct for reading, arrow function syntax is valid, 'end' event is optional here.
  3. Final Answer:

    Missing parentheses after toString method call -> Option D
  4. Quick Check:

    Methods need () to run [OK]
Hint: Remember to call methods with () [OK]
Common Mistakes:
  • Forgetting parentheses on methods
  • Confusing read and write streams
  • Thinking 'end' event is mandatory for reading
5. You want to process a huge log file line by line without loading it all into memory. Which approach best uses streams to achieve this efficiently?
hard
A. Use fs.readFile to load entire file then split by lines
B. Use fs.createReadStream and split data chunks manually by newline characters
C. Use fs.createWriteStream to write lines one by one
D. Use fs.open and read fixed-size buffers without streaming

Solution

  1. Step 1: Understand memory constraints with large files

    Loading entire file with readFile uses too much memory for huge files.
  2. Step 2: Using streams to process line by line

    createReadStream reads file in chunks; splitting chunks by newline lets you process lines without full load.
  3. Step 3: Why other options are less efficient

    createWriteStream is for writing, not reading; fs.open with manual buffer reads is complex and less efficient.
  4. Final Answer:

    Use fs.createReadStream and split data chunks manually by newline characters -> Option B
  5. Quick Check:

    Streams + chunk splitting = memory efficient line processing [OK]
Hint: Stream chunks and split by newline for big file lines [OK]
Common Mistakes:
  • Loading whole file with readFile
  • Using write stream to read data
  • Ignoring chunk boundaries when splitting lines