0
0
Node.jsframework~10 mins

Stream types (Readable, Writable, Transform, Duplex) in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

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

Node.js
const { Readable } = require('stream');
const readable = [1] Readable({
  read() {
    this.push('Hello');
    this.push(null);
  }
});
Drag options to blanks, or click blank then click option'
Acreate
Bnew
Cfrom
Dpipe
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'from' expects an iterable argument, not an options object with read().
Trying to use 'pipe' here is incorrect.
2fill in blank
medium

Complete the code to write data to a writable stream.

Node.js
const { Writable } = require('stream');
const writable = new Writable({
  write(chunk, encoding, callback) {
    console.log(chunk.toString());
    [1]();
  }
});
Drag options to blanks, or click blank then click option'
Aend
Bwrite
Ccallback
Dpush
Attempts:
3 left
💡 Hint
Common Mistakes
Calling 'write' inside 'write' causes infinite recursion.
Forgetting to call 'callback' causes the stream to hang.
3fill in blank
hard

Fix the error in the transform stream by completing the missing method call.

Node.js
const { Transform } = require('stream');
const transform = new Transform({
  transform(chunk, encoding, callback) {
    this.push(chunk.toString().toUpperCase());
    [1]();
  }
});
Drag options to blanks, or click blank then click option'
Apush
Bend
Cwrite
Dcallback
Attempts:
3 left
💡 Hint
Common Mistakes
Calling 'write' or 'push' instead of 'callback' causes errors.
Forgetting to call 'callback' causes the stream to stall.
4fill in blank
hard

Fill both blanks to create a duplex stream that echoes input data.

Node.js
const { Duplex } = require('stream');
const duplex = new Duplex({
  read(size) {
    // No data to push here
  },
  write(chunk, encoding, [1]) {
    this.push(chunk);
    [2]();
  }
});
Drag options to blanks, or click blank then click option'
Acallback
Bwrite
Cend
Dread
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'write' or 'end' instead of 'callback' causes errors.
Not calling the callback causes the stream to hang.
5fill in blank
hard

Fill all three blanks to create a transform stream that reverses input strings.

Node.js
const { Transform } = require('stream');
const reverse = new Transform({
  transform(chunk, encoding, [1]) {
    const input = chunk.toString();
    const reversed = input.split('').[2]().join('');
    this.push(reversed);
    [3]();
  }
});
Drag options to blanks, or click blank then click option'
Acallback
Breverse
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'reverse' as a function argument name causes errors.
Forgetting to call callback causes the stream to stall.