0
0
Node.jsframework~10 mins

Why file system access matters in Node.js - Test Your Understanding

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

Complete the code to import the Node.js file system module.

Node.js
const fs = require([1]);
Drag options to blanks, or click blank then click option'
A"fs"
B"path"
C"http"
D"os"
Attempts:
3 left
💡 Hint
Common Mistakes
Using other module names like "path" or "http" which do not provide file system access.
2fill in blank
medium

Complete the code to read a file asynchronously using the fs module.

Node.js
fs.readFile('example.txt', 'utf8', (err, data) => {
  if (err) throw err;
  console.log([1]);
});
Drag options to blanks, or click blank then click option'
Adata
Berr
Cfs
Dconsole
Attempts:
3 left
💡 Hint
Common Mistakes
Logging err instead of data.
Trying to log fs or console which are not file content.
3fill in blank
hard

Fix the error in the code to write data to a file asynchronously.

Node.js
fs.writeFile('output.txt', [1], (err) => {
  if (err) throw err;
  console.log('File saved!');
});
Drag options to blanks, or click blank then click option'
Aconsole.log
Bfs
Cundefined
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Passing fs or console.log instead of the data string.
Passing undefined which causes an error.
4fill in blank
hard

Fill both blanks to create a directory and then read its contents.

Node.js
fs.[1]('myFolder', (err) => {
  if (err) throw err;
  fs.[2]('myFolder', (err, files) => {
    if (err) throw err;
    console.log(files);
  });
});
Drag options to blanks, or click blank then click option'
Amkdir
BreadFile
Creaddir
Dunlink
Attempts:
3 left
💡 Hint
Common Mistakes
Using readFile which reads file content, not directory contents.
Using unlink which deletes files.
5fill in blank
hard

Fill all three blanks to check if a file exists, then read it if it does.

Node.js
fs.[1]('data.txt', (err) => {
  if (!err) {
    fs.[2]('data.txt', 'utf8', (err, [3]) => {
      if (err) throw err;
      console.log([3]);
    });
  } else {
    console.log('File does not exist');
  }
});
Drag options to blanks, or click blank then click option'
Aaccess
BreadFile
Cdata
DwriteFile
Attempts:
3 left
💡 Hint
Common Mistakes
Using writeFile instead of readFile to read content.
Not checking if the file exists before reading.