0
0
Node.jsframework~10 mins

Watching files for changes 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 built-in module for watching files.

Node.js
const fs = require('[1]');
Drag options to blanks, or click blank then click option'
Afs
Bpath
Cfs-extra
Dfilewatch
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent module name like 'filewatch'.
Confusing 'path' module with 'fs'.
2fill in blank
medium

Complete the code to watch a file named 'example.txt' for changes.

Node.js
fs.[1]('example.txt', (eventType, filename) => {
  if (filename) {
    console.log(`${filename} file changed`);
  }
});
Drag options to blanks, or click blank then click option'
AreadFile
Bwatch
Copen
DwriteFile
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'readFile' which reads file content once.
Using 'writeFile' which writes data to a file.
3fill in blank
hard

Fix the error in the callback parameter name to correctly detect the changed file.

Node.js
fs.watch('data.json', (eventType, [1]) => {
  if (filename) {
    console.log(`${filename} was modified`);
  }
});
Drag options to blanks, or click blank then click option'
AfileName
Bfile
Cfilename
Dfile_path
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fileName' with uppercase N which is undefined.
Using a different variable name than inside the function.
4fill in blank
hard

Fill both blanks to watch a directory named 'logs' and log the event type and filename.

Node.js
fs.[1]('logs', ([2], filename) => {
  console.log(`Event: ${eventType}, File: ${filename}`);
});
Drag options to blanks, or click blank then click option'
Awatch
Bchange
CeventType
Dopen
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'change' as a method name instead of 'watch'.
Using 'change' as a parameter name instead of 'eventType'.
5fill in blank
hard

Fill all three blanks to watch 'config.json', check if the event is 'rename', and log the filename.

Node.js
fs.watch('config.json', ([1], [2]) => {
  if (eventType === [3]) {
    console.log(`File ${filename} was renamed.`);
  }
});
Drag options to blanks, or click blank then click option'
AeventType
Bfilename
C'rename'
D'change'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of parameters.
Checking eventType against 'change' instead of 'rename'.