A REST API is made with NodeJS, Express, MongoDB Atlas database.
API can store name, birthday, email, phone, address to the database.
Before you begin, ensure you have met the following requirements:
-
Node.js v18: This project requires Node.js version 18. If you don't have it installed, you can download and install it from the official Node.js website.
-
MongoDB Atlas Database: This project uses MongoDB Atlas Database. You can create it on the official MongoDB website.
-
Clone this repository:
git clone https://github.com/samu20108/contact-manager-api
-
Install dependencies with npm:
cd contact-manager-api npm install
-
Copy or rename
example.env
to a new file named.env
. Add your Atlas connection string and Database name to the file. -
Run the application:
npm run dev-start
Add a new contact:
POST localhost:5050/contacts/add
body:
{
"_id": "65453e71538ab1b158a33246",
"first_name": "John",
"last_name": "Doe",
"birthday": {
"year": 1990,
"month": 6,
"day": 15
},
"email": "[email protected]",
"phone": "+1 123-456-7890",
"address": {
"street_address": "123 Main Street",
"city": "City",
"country": "Country"
},
}
Fetch all contacts:
GET localhost:5050/contacts
[
{
"_id": "65453e71538ab1b158a33246",
"first_name": "John",
"last_name": "Doe",
"birthday": {
"year": 1990,
"month": 6,
"day": 15
},
"email": "[email protected]",
"phone": "+1 123-456-7890",
"address": {
"street_address": "123 Main Street",
"city": "City",
"country": "Country"
},
"age": 33
},
]
*GET /contacts
: Retrieve all contacts.
*GET /contacts/id/{id}
: Retrieve contact by id.
*GET /contacts/birthdays-next-month
: Retrieve contacts whose birthday is next month
*GET /contacts/next-birthday
: Retrieve contact whose birthday is coming next
*GET /contacts/add-age-fields
: Calculate ages and add age fields if no exist
*POST /contacts/add
: Create a new contact.
*DELETE /contacts/{id}
: Delete a contact by ID.
*PUT /contacts/edit
: Edit a contact by ID.
src/
|-- db/
| |-- conn.mjs
|-- routes/
| |-- contacts.mjs
|-- sample-data
| |-- sample-contacts-data.json
|-- example.env
|-- index.mjs
|-- loadEnv.mjs
|-- package-lock.json
|-- package.json
|-- README.md