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

Conteúdo do Curso

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

bookConsole App: DirInspect Pro

This chapter presents you with a challenge: creating an advanced console app named DirInspect Pro. This app will empower you to thoroughly analyze any directory and gain insightful statistics about its files and subdirectories.

🏆 Challenge Awaits

Picture a scenario where you must navigate a labyrinth of folders containing crucial files and data. DirInspect Pro is your ally in this journey, providing comprehensive insights into the directory's structure and contents.

🚀 The Resulting App

Get ready to wield DirInspect Pro's capabilities. The app will furnish you with critical information, such as

  • The total number of items;
  • The aggregate size of all items;
  • The largest file's name and size;
  • The detailed list of individual file names and sizes.

Two Paths to Choose

You have two paths ahead.

  • The first is to tackle this challenge head-on, honing your skills without guidance;
  • The second is to follow a helpful guide that guarantees your success.

Whichever path you choose, you're in for a rewarding journey culminating in creating a captivating and functional console app.

Masterplan

  • 👉 Step 1: Import Required Modules;
  • 👉 Step 2: Define getStats Function;
  • 👉 Step 3: Define analyzeFile Function;
  • 👉 Step 4: Define analyzeDirectory Function;
  • 👉 Step 5: Define main Function and Invoke;
  • 🎉 Conclusion;
  • 🏁 Full App Code.

Step 1: Import Required Modules

To embark on this adventure, you'll need the right tools. Begin by importing two key modules: fs.promises to manage the file system asynchronously and path to handle file paths effectively.

Step 2: Define getStats Function

The asynchronous function, getStats, takes a file or directory path as an argument and attempts to retrieve its statistics using fs.stat.

  • If successful, it returns the statistics;
  • If an error occurs, it logs an error message and returns null.

Step 3: Define analyzeFile Function

The analyzeFile function uses the getStats function to obtain statistics for a file. If statistics are available (not null), it returns an object containing the file's name (extracted using path.basename) and its size.

Step 4: Define analyzeDirectory Function

The analyzeDirectory function takes a directory path as an argument and comprehensively analyzes its contents.

  • It begins by reading the items within the directory using fs.readdir and then iterates through each item;
  • For each item, it constructs the full path using path.join and retrieves its statistics using the getStats function;
  • If the stats indicate that the item is a file, it updates file-related statistics;
  • If the item is a subdirectory, it recursively calls the analyzeDirectory function to analyze its contents and then aggregates the statistics.

Step 5: Define main Function and Invoke

The main function is the entry point of the script. It specifies the directory path to analyze (in this case, ./docs), calls the analyzeDirectory function to obtain the statistics of the directory and its contents, and then outputs the collected information. The function prints out

  • The total number of items;
  • The total number of files;
  • The total size;
  • The details about the largest file;
  • The list of files in the directory.

🎉 Conclusion: Mastered Skills

With DirInspect Pro, you've mastered the art of analyzing directories like a pro. This console app showcases your ability to extract file statistics, handle errors seamlessly, and unveil meaningful insights about files and subdirectories within a specified directory.

👨‍💻 Full App Code

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 10
some-alt