0
0
Node.jsframework~10 mins

Appending to files 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 append text to a file using Node.js.

Node.js
import { appendFile } from 'fs/promises';

await appendFile('log.txt', [1]);
Drag options to blanks, or click blank then click option'
A'New entry\n'
BreadFile('log.txt')
C'readme.md'
Dconsole.log('append')
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a function call instead of a string as the second argument.
Using the wrong filename or forgetting to add a newline character.
2fill in blank
medium

Complete the code to append 'Hello World' with a newline to 'output.txt'.

Node.js
import { appendFile } from 'fs/promises';

await appendFile('output.txt', [1]);
Drag options to blanks, or click blank then click option'
A'Hello World'
B'HelloWorld\n'
C'Hello World\n'
D'Hello\nWorld'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the newline character at the end.
Adding newline in the middle of the string.
3fill in blank
hard

Fix the error in the code to append text asynchronously.

Node.js
import fs from 'fs';

fs.appendFile('data.txt', [1], (err) => {
  if (err) throw err;
  console.log('Saved!');
});
Drag options to blanks, or click blank then click option'
A'More data\n'
Bawait 'More data\n'
Cfs.readFile('data.txt')
Dconsole.log('More data')
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'await' inside the callback version of appendFile.
Passing a function or console.log instead of a string.
4fill in blank
hard

Fill both blanks to append 'Log entry' with a newline to 'app.log' using promises.

Node.js
import { [1] } from 'fs/promises';

await [2]('app.log', 'Log entry\n');
Drag options to blanks, or click blank then click option'
AappendFile
BreadFile
Cappend
DwriteFile
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'writeFile' which overwrites instead of appending.
Using 'readFile' which reads but does not write.
5fill in blank
hard

Fill all three blanks to append 'Error: ' plus a variable message and newline to 'errors.log'.

Node.js
import { [1] } from 'fs/promises';

const message = 'File not found';
await [2]('errors.log', [3] + message + '\n');
Drag options to blanks, or click blank then click option'
AappendFile
BwriteFile
C'Error: '
DreadFile
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'writeFile' which overwrites the file.
Forgetting to add the 'Error: ' prefix string.