Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: File System Operations | Building Console Applications with Node.js
Backend Development with Node.js and Express.js

Challenge: File System Operations

Swipe to show menu

Goal

Master the art of task management automation! Your mission is to develop an application that gathers tasks from one source, extracts their content, and integrates them into another file. Your solution should also handle any potential errors along the way.

Task

Imagine you have two files:

  • tasks.txt, which contains a list of existing tasks;
  • new-task.txt, which contains a single task that should be added to tasks.txt.

Follow these steps to complete the challenge:

  1. Prepare your workspace

    • Create a new folder on your machine;
    • Open it in your preferred code editor.
  2. Create tasks.txt

    • Create a file named tasks.txt;

    • Add the following tasks to it, or download the prepared file:

      tasks.txt

    Contents:

    • Teach a goldfish to play chess;
    • Build a sandcastle in your living room;
    • Create a song 🎶 using only sounds from nature.
  3. Create new-task.txt

    • Create a file named new-task.txt;

    • Add the following task to it, or download the prepared file:

      new-task.txt

    Content:

    • Invent a new dance move and perform it in public.
  4. Create app.js

    • Import the fs module to work with files;
    • Use readFile() to read the content of new-task.txt;
    • Handle successful and failed operations using .then() and .catch();
    • Inside the .then() block, use appendFile() to add the content to tasks.txt;
    • Append a newline character (\n) after the inserted task.
  5. Run the application

    • Save app.js;
    • Open the terminal and run:
    node app
    

If you prefer to use the code editor below, keep in mind that it does not recognize your files and will not show your progress.

123456789
const fs = require("fs").___; fs.___("new-task.txt", "utf-8") .then(___ => { return fs.___("tasks.txt", ___ + ___); }) .___((error) => { console.log("Error:", error); });
Hint
expand arrow
  1. Import the fs module to work with files.
  2. Use the fs.readFile("new-task.txt", "utf-8")to read content from the file new-task.txt.
  3. Chain a .then() to handle the successful reading of the file content.
  4. In the .then() block, provide a callback function that receives the content as an argument.
  5. Inside the .then() block of the readFile promise, return the result of using fs.appendFile to add content to the file tasks.txt.
  6. Remember to include a newline character \n to separate the appended content.
  7. After the .then() block of the appendFile operation, chain a .catch() block to handle any errors that might occur in the entire chain.
  8. In the .catch() block, provide a callback function that receives an error parameter.
  9. Inside the .catch() block, log an error message along with the received error.
  10. Run the script using Node.js by typing node app.
Solution
expand arrow
const fs = require("fs").promises;

fs.readFile("new-task.txt", "utf-8")
  .then((content) => {
    return fs.appendFile("tasks.txt", content + "\n");
  })
  .catch((error) => {
    console.log("Error occurred:", error);
  });
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 4

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Challenge: File System Operations

Goal

Master the art of task management automation! Your mission is to develop an application that gathers tasks from one source, extracts their content, and integrates them into another file. Your solution should also handle any potential errors along the way.

Task

Imagine you have two files:

  • tasks.txt, which contains a list of existing tasks;
  • new-task.txt, which contains a single task that should be added to tasks.txt.

Follow these steps to complete the challenge:

  1. Prepare your workspace

    • Create a new folder on your machine;
    • Open it in your preferred code editor.
  2. Create tasks.txt

    • Create a file named tasks.txt;

    • Add the following tasks to it, or download the prepared file:

      tasks.txt

    Contents:

    • Teach a goldfish to play chess;
    • Build a sandcastle in your living room;
    • Create a song 🎶 using only sounds from nature.
  3. Create new-task.txt

    • Create a file named new-task.txt;

    • Add the following task to it, or download the prepared file:

      new-task.txt

    Content:

    • Invent a new dance move and perform it in public.
  4. Create app.js

    • Import the fs module to work with files;
    • Use readFile() to read the content of new-task.txt;
    • Handle successful and failed operations using .then() and .catch();
    • Inside the .then() block, use appendFile() to add the content to tasks.txt;
    • Append a newline character (\n) after the inserted task.
  5. Run the application

    • Save app.js;
    • Open the terminal and run:
    node app
    

If you prefer to use the code editor below, keep in mind that it does not recognize your files and will not show your progress.

123456789
const fs = require("fs").___; fs.___("new-task.txt", "utf-8") .then(___ => { return fs.___("tasks.txt", ___ + ___); }) .___((error) => { console.log("Error:", error); });
Hint
expand arrow
  1. Import the fs module to work with files.
  2. Use the fs.readFile("new-task.txt", "utf-8")to read content from the file new-task.txt.
  3. Chain a .then() to handle the successful reading of the file content.
  4. In the .then() block, provide a callback function that receives the content as an argument.
  5. Inside the .then() block of the readFile promise, return the result of using fs.appendFile to add content to the file tasks.txt.
  6. Remember to include a newline character \n to separate the appended content.
  7. After the .then() block of the appendFile operation, chain a .catch() block to handle any errors that might occur in the entire chain.
  8. In the .catch() block, provide a callback function that receives an error parameter.
  9. Inside the .catch() block, log an error message along with the received error.
  10. Run the script using Node.js by typing node app.
Solution
expand arrow
const fs = require("fs").promises;

fs.readFile("new-task.txt", "utf-8")
  .then((content) => {
    return fs.appendFile("tasks.txt", content + "\n");
  })
  .catch((error) => {
    console.log("Error occurred:", error);
  });
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 4
some-alt