Зміст курсу
Backend Development with Node.js and Express.js
Backend Development with Node.js and Express.js
Challenge: 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:
- Prepare Your Workspace: Start by creating a new folder on your machine and open it using your favorite code editor.
- 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 🌿
.
- 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. 💃🕺
.
- 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 thefs
module to extract the content from thenew-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 theappendFile
function to add the content to thetasks.txt
file. Don't forget to append a newline character (\n
) after the content.
- Import fs Module: Begin by importing the
- Run the Magic: Save your
app.js
file and execute it using Node.js in the terminal with the commandnode 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.
const fs = require("fs").___; fs.___("new-task.txt", "utf-8") .then(___ => { return fs.___("tasks.txt", ___ + ___); }) .___((error) => { console.log("Error:", error); });
Дякуємо за ваш відгук!