Skip to content

RahulHarihar/mycontacts-backend

Repository files navigation

📇 Contact Management App

A simple Node.js + Express + MongoDB application for managing contacts with JWT authentication and user-based access control.


🚀 Project Introduction

This project demonstrates how to build a REST API using Express with the following features:

  • REST API convention (clear endpoints, CRUD structure)
  • Contact management (CRUD)
  • User authentication with JWT
  • Route protection and role-based access
  • MongoDB integration with Mongoose
  • Error handling middleware and async handlers

⚙️ Project Setup

1. Clone the repository

git clone <your-repo-url>
cd contact-management-app

2. Install dependencies

npm install

3. Setup environment variables

Create a .env file in the root directory:

PORT=5000
MONGO_URI=your_mongodb_connection_string
JWT_SECRET=your_secret_key

4. Run the project

npm start

🛠 Features & Implementation

Express Setup

  • Built an Express server
  • Configured middleware for JSON parsing and error handling
  • Used express.Router() for modular route management

Contacts CRUD

  • GET /api/contacts → Fetch all contacts
  • POST /api/contacts → Create a new contact
  • GET /api/contacts/:id → Fetch a single contact by ID
  • PUT /api/contacts/:id → Update a contact
  • DELETE /api/contacts/:id → Delete a contact

Controllers

  • Contact Controller → Handles CRUD logic for contacts
  • User Controller → Handles registration, login, and current user retrieval

Middleware

  • Custom error handling middleware
  • Async handler (express-async-handler) for managing async/await
  • JWT verification middleware (verifyJWT) for protecting routes

MongoDB & Mongoose

  • Mongoose schema for Contact
  • Mongoose schema for User with password hashing
  • Established relationship between User and Contact

Authentication & Authorization

  • User registration (/api/users/register)
  • User login with JWT access token (/api/users/login)
  • Route protection with JWT verification
  • User-specific contacts → Only logged-in users can manage their own contacts

Testing API

You can use Thunder Client (VS Code extension) or Postman:

  1. Register a user → /api/users/register
  2. Login with credentials → /api/users/login
    • Copy the JWT token
  3. Add Authorization: Bearer <your_token> in headers
  4. Access protected routes (Contacts CRUD)

Learning Highlights

  • Building an Express server
  • REST API conventions
  • Using routers for modular code
  • Middleware for error handling and async requests
  • MongoDB integration with Mongoose
  • User authentication with JWT
  • Protecting routes with middleware
  • Relationship between users and contacts in MongoDB

Tech Stack

  • Node.js
  • Express.js
  • MongoDB + Mongoose
  • JWT (jsonwebtoken)
  • bcryptjs

License

This project is for learning purposes and can be freely used or extended.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors