Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: FileSystem | Console Applications
Backend Development with Node.js and Express.js
course content

Course Content

Backend Development with Node.js and Express.js

Backend Development with Node.js and Express.js

1. Introduction
2. Console Applications
3. Express.js Framework
4. Developing REST API

bookChallenge: 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.

123456789
const fs = require("fs").___; fs.___("new-task.txt", "utf-8") .then(___ => { return fs.___("tasks.txt", ___ + ___); }) .___((error) => { console.log("Error:", error); });
copy

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 4
some-alt