Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: FileSystem | Console Applications
Node.js Express: API & CLI Apps

Challenge: FileSystemChallenge: FileSystem

🏆 Challenge

🎯 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, and new-task.txt, which includes a single task that must be added to the tasks.txt file.

Follow these steps to complete the challenge and create the real deal on your machine:

  1. Prepare Your Workspace: Start by creating a new folder on your machine and open it using your favorite code editor.
  2. Setup Tasks: Create the tasks.txt file and populate it with the following tasks or use the provided tasks.txt file:
    • Teach a goldfish 🐠 to play chess ♟️;
    • Build a sandcastle 🏰 in your living room 🛋️;
    • Create a song 🎶 using only sounds from nature 🌿.
  3. Define New Task: Create the new-task.txt file and insert the following task or use the provided new-task.txt file:
    • Invent a new dance move and perform it in public. 💃🕺.
  4. Main Script: Craft the app.js file, which will serve as the heart of your application.
    • Import fs Module: Begin by importing the fs module to enable file handling within your application;
    • Read Content: Utilize the readFile function from the fs module to extract the content from the new-task.txt file. Be sure to implement .then() and .catch() to manage both success and error scenarios;
    • Append Content: Inside the .then() block, once the content is successfully read, employ the appendFile function to add the content to the tasks.txt file. Don't forget to append a newline character (\n) after the content.
  5. Run the Magic: Save your app.js file and execute it using Node.js in the terminal with the command 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.

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.

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);
  });
      

Все було зрозуміло?

Секція 2. Розділ 4

Node.js Express: API & CLI Apps

Challenge: FileSystemChallenge: FileSystem

🏆 Challenge

🎯 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, and new-task.txt, which includes a single task that must be added to the tasks.txt file.

Follow these steps to complete the challenge and create the real deal on your machine:

  1. Prepare Your Workspace: Start by creating a new folder on your machine and open it using your favorite code editor.
  2. Setup Tasks: Create the tasks.txt file and populate it with the following tasks or use the provided tasks.txt file:
    • Teach a goldfish 🐠 to play chess ♟️;
    • Build a sandcastle 🏰 in your living room 🛋️;
    • Create a song 🎶 using only sounds from nature 🌿.
  3. Define New Task: Create the new-task.txt file and insert the following task or use the provided new-task.txt file:
    • Invent a new dance move and perform it in public. 💃🕺.
  4. Main Script: Craft the app.js file, which will serve as the heart of your application.
    • Import fs Module: Begin by importing the fs module to enable file handling within your application;
    • Read Content: Utilize the readFile function from the fs module to extract the content from the new-task.txt file. Be sure to implement .then() and .catch() to manage both success and error scenarios;
    • Append Content: Inside the .then() block, once the content is successfully read, employ the appendFile function to add the content to the tasks.txt file. Don't forget to append a newline character (\n) after the content.
  5. Run the Magic: Save your app.js file and execute it using Node.js in the terminal with the command 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.

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.

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);
  });
      

Все було зрозуміло?

Секція 2. Розділ 4
some-alt