This project is the first Node.js homework assignment. The goal of the project is to build a small command-line application that works with files and manages a list of contacts.
The application reads and writes data to a JSON file and allows users to generate, add, list, count, and remove contacts using simple npm scripts.
-
GitHub Repository:
https://github.com/kutluhangil/nodejs-hw-01
The objective of this homework is to learn the basics of Node.js and how to work with files using JavaScript.
Students start from a prepared project template and gradually implement different scripts that interact with a contacts database stored in a JSON file.
Each step introduces a new concept such as reading files, writing data, generating fake data, and manipulating arrays stored in files.
- Node.js
- JavaScript (ES Modules)
- File System (fs/promises)
- @faker-js/faker
- ESLint
- Prettier
src/ ├── constants/ │ └── contacts.js │ ├── db/ │ └── db.json │ ├── scripts/ │ ├── addOneContact.js │ ├── countContacts.js │ ├── generateContacts.js │ ├── getAllContacts.js │ ├── removeAllContacts.js │ └── removeLastContact.js │ ├── utils/ │ ├── createFakeContact.js │ ├── readContacts.js │ └── writeContacts.js │ └── index.js
Install project dependencies:
npm install
Install faker library used to generate random contacts:
npm i -D @faker-js/faker
The following npm commands run different scripts in the project:
-
Generate Contacts
Creates multiple random contacts and saves them to the database.npm run generate
-
Add One Contact
Adds a single randomly generated contact.npm run add-one
-
Get All Contacts
Reads and displays all contacts from the database.npm run get-all
-
Count Contacts
Returns the total number of contacts stored in the database.npm run count
-
Remove Last Contact
Deletes the last contact in the list.npm run remove-last
-
Remove All Contacts
Deletes every contact from the database.npm run remove-all
All contacts are stored inside the following file:
src/db/db.json
The application reads data from this file, modifies the array of contacts, and writes the updated data back to the file.
Random contacts are generated using the @faker-js/faker library.
- Create a repository named nodejs-hw-01
- Push all source code to GitHub
- The application must run without errors
- All scripts must correctly manipulate the contacts file
This project introduces fundamental Node.js concepts such as working with files, modular project structure, and executing scripts through npm commands.
It serves as the foundation for more advanced backend development tasks.
Happy coding! 🚀