0
0
Node.jsframework~10 mins

Writing data with Writable streams 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 writable stream to a file named 'output.txt'.

Node.js
import { createWriteStream } from 'fs';
const writable = createWriteStream([1]);
Drag options to blanks, or click blank then click option'
A'readme.md'
B'input.txt'
C'data.json'
D'output.txt'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a filename meant for reading instead of writing.
Forgetting to put the filename in quotes.
2fill in blank
medium

Complete the code to write the string 'Hello, world!' to the writable stream.

Node.js
writable.[1]('Hello, world!');
Drag options to blanks, or click blank then click option'
Apipe
Bwrite
Cend
Dread
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'read' which is for readable streams.
Using 'end' which closes the stream immediately.
3fill in blank
hard

Fix the error in the code to properly close the writable stream after writing.

Node.js
writable.write('Data');
writable.[1]();
Drag options to blanks, or click blank then click option'
Aend
Bfinish
Cclose
Ddestroy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'close' which is not a method on writable streams.
Using 'finish' which is an event, not a method.
4fill in blank
hard

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

Node.js
import { [1] } from 'fs';
const stream = [2]('test.txt');
stream.write('Test');
Drag options to blanks, or click blank then click option'
AcreateWriteStream
BcreateReadStream
CwriteFileSync
DreadFileSync
Attempts:
3 left
💡 Hint
Common Mistakes
Using read stream functions instead of write stream.
Using synchronous file write functions which do not create streams.
5fill in blank
hard

Fill all three blanks to write 'Hello' to a file and close the stream properly.

Node.js
import { [1] } from 'fs';
const ws = [2]('hello.txt');
ws.[3]('Hello');
ws.end();
Drag options to blanks, or click blank then click option'
AcreateReadStream
BcreateWriteStream
Cwrite
DreadFileSync
Attempts:
3 left
💡 Hint
Common Mistakes
Using read stream functions instead of write stream.
Using 'readFileSync' which is not related to streams.
Using 'createReadStream' instead of 'createWriteStream'.