0
0
Node.jsframework~10 mins

Reading files asynchronously with callbacks 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 import the file system module.

Node.js
const fs = require([1]);
Drag options to blanks, or click blank then click option'
A"fs"
B"file"
C"filesystem"
D"path"
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect module names like "file" or "filesystem".
Forgetting the quotes around the module name.
2fill in blank
medium

Complete the code to read a file asynchronously with a callback.

Node.js
fs.readFile('example.txt', 'utf8', [1]);
Drag options to blanks, or click blank then click option'
Afunction() { console.log('done'); }
Bconsole.log
C(err, data) => { if (err) throw err; console.log(data); }
D() => console.log('read')
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a callback without parameters.
Not handling the error parameter.
3fill in blank
hard

Fix the error in the callback function to properly handle errors.

Node.js
fs.readFile('data.txt', 'utf8', ([1], content) => { if ([1]) throw [1]; console.log(content); });
Drag options to blanks, or click blank then click option'
Aerror
Berr
Ce
Dexception
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name in the parameter and in the error check.
Not checking the error parameter at all.
4fill in blank
hard

Fill both blanks to read a file and handle errors properly.

Node.js
fs.readFile('log.txt', [1], ([2], data) => { if (err) throw err; console.log(data); });
Drag options to blanks, or click blank then click option'
A'utf8'
Berr
Cerror
D'ascii'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong encoding like 'ascii' when 'utf8' is expected.
Mismatching error parameter name and check.
5fill in blank
hard

Fill all three blanks to read a file asynchronously and log errors or data.

Node.js
fs.readFile([1], [2], ([3], content) => { if (err) console.error(err); else console.log(content); });
Drag options to blanks, or click blank then click option'
A'notes.txt'
B'utf8'
Cerr
D'binary'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong file name or encoding.
Not naming the error parameter correctly.