Welcome to the Contact Manager project! This application allows users to manage their contacts efficiently. Built with Node.js, Express, and MongoDB, it offers a robust backend to handle CRUD operations and user authentication.
- Features
- Tech Stack
- Getting Started
- Configuration
- Folder Structure
- API Endpoints
- Authentication
- Testing APIs
- Running the Project
- Contributing
- License
- CRUD Operations: Create, Read, Update, and Delete contacts.
- User Authentication: Secure endpoints using JWT authentication.
- Environment Configurations: Manage sensitive information using environment variables.
- Backend: Node.js, Express
- Database: MongoDB
- Authentication: JWT (JSON Web Tokens)
- Environment Management: dotenv
Ensure you have the following installed:
-
Clone the repository:
git clone https://github.com/yourusername/contact-manager.git
-
Navigate to the project directory:
cd contact-manager
-
Install the required dependencies:
npm install
Create a .env
file in the root directory of the project and add the following environment variables:
PORT=5000
CONNECTION_STRING=mongodb+srv://<username>:<password>@<cluster-url>/mycontacts-backend?retryWrites=true&w=majority&appName=<appName>
ACCESS_TOKEN_SECRET=<your_jwt_secret>
Replace <username>
, <password>
, <cluster-url>
, and <appName>
with your MongoDB credentials and cluster details. Set your own JWT secret for ACCESS_TOKEN_SECRET
.
Here's a brief overview of the project structure:
contact-manager/
│
├── config/ # Configuration files (e.g., DB connection, JWT)
│ └── db.js # MongoDB connection setup
│
├── controllers/ # Request handlers
│ ├── authController.js
│ └── contactController.js
│
├── middleware/ # Middleware functions
│ └── authMiddleware.js # JWT authentication middleware
│
├── models/ # Mongoose models
│ ├── User.js
│ └── Contact.js
│
├── routes/ # Express routes
│ ├── authRoutes.js
│ └── contactRoutes.js
│
├── .env # Environment variables
├── app.js # Entry point of the application
├── package.json # Project metadata and dependencies
└── README.md # Project documentation
- POST
/api/auth/register
- Register a new user - POST
/api/auth/login
- Authenticate a user and get a JWT
- GET
/api/contacts
- Get all contacts (requires authentication) - POST
/api/contacts
- Create a new contact (requires authentication) - GET
/api/contacts/:id
- Get a specific contact by ID (requires authentication) - PUT
/api/contacts/:id
- Update a contact by ID (requires authentication) - DELETE
/api/contacts/:id
- Delete a contact by ID (requires authentication)
JWT authentication is used to secure the API endpoints. Here's a brief overview of how it works:
- User Registration: Users can register by sending a
POST
request to/api/auth/register
with their credentials. - User Login: Users authenticate by sending a
POST
request to/api/auth/login
. Upon successful login, they receive a JWT token. - Access Protected Routes: Include the JWT token in the
Authorization
header (asBearer <token>
) for accessing protected routes.
The JWT token is used to verify the identity of the user and ensure they have the right to access specific resources.
You can test the APIs using tools like Thunder Client and Insomnia. Here’s how:
- Install Thunder Client: You can find it in the VS Code extensions marketplace.
- Set Up Requests: Create new requests for each endpoint with the appropriate HTTP method.
- Add Headers: For protected routes, include the
Authorization
header with the valueBearer <your_token>
.
- Install Insomnia: Download and install Insomnia from Insomnia.
- Create Requests: Add new requests for each API endpoint.
- Set Authentication: Use the JWT token for authentication by adding it in the
Authorization
header with the valueBearer <your_token>
.
To start the application, run:
npm start
The server will run on http://localhost:5000
by default.
Feel free to open an issue or submit a pull request if you want to contribute to the project. Please follow the guidelines outlined in the CONTRIBUTING.md file.
This project is licensed under the MIT License - see the LICENSE file for details.