0
0
Node.jsframework~10 mins

Buffer and streams relationship 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 Buffer from a string.

Node.js
const buf = Buffer.[1]('Hello');
Drag options to blanks, or click blank then click option'
Afrom
Balloc
Cwrite
Dslice
Attempts:
3 left
💡 Hint
Common Mistakes
Using Buffer.alloc instead of Buffer.from
Trying to use Buffer.write directly
2fill in blank
medium

Complete the code to read data from a readable stream into a Buffer.

Node.js
stream.on('data', chunk => {
  const buf = Buffer.[1](chunk);
});
Drag options to blanks, or click blank then click option'
Aalloc
Bslice
Cconcat
Dfrom
Attempts:
3 left
💡 Hint
Common Mistakes
Using Buffer.alloc which creates empty buffer
Trying to use Buffer.concat on single chunk
3fill in blank
hard

Fix the error in the code to properly concatenate multiple Buffer chunks from a stream.

Node.js
let buffers = [];
stream.on('data', chunk => {
  buffers.push(chunk);
});
stream.on('end', () => {
  const result = Buffer.[1](buffers);
});
Drag options to blanks, or click blank then click option'
Afrom
Bconcat
Calloc
Dslice
Attempts:
3 left
💡 Hint
Common Mistakes
Using Buffer.from on array of buffers
Using Buffer.alloc which creates empty buffer
4fill in blank
hard

Fill both blanks to create a writable stream that writes Buffer data to a file.

Node.js
const fs = require('fs');
const writable = fs.createWriteStream('output.txt');
writable.[1](Buffer.[2]('Data to write'));
Drag options to blanks, or click blank then click option'
Awrite
Bfrom
Calloc
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using Buffer.alloc instead of Buffer.from
Using end instead of write for sending data
5fill in blank
hard

Fill all three blanks to read data from a readable stream, concatenate buffers, and convert to string.

Node.js
let chunks = [];
readable.[1]('data', chunk => {
  chunks.[2](chunk);
});
readable.on('end', () => {
  const buffer = Buffer.[3](chunks);
  console.log(buffer.toString());
});
Drag options to blanks, or click blank then click option'
Aon
Bpush
Cconcat
Dwrite
Attempts:
3 left
💡 Hint
Common Mistakes
Using write instead of on for event
Using alloc instead of concat
Using pop instead of push