Skip to content

Mohammed-Er/E-Commerce-API

Repository files navigation

🛒 E-Commerce REST API

A secure RESTful E-Commerce Backend API built with Node.js, Express.js, and MongoDB following the MVC Architecture.

The API provides JWT Authentication, Role-Based Access Control (RBAC), product management, shopping cart functionality, and is designed as the backend for a modern e-commerce application.


🎯 Project Goal

The API enables users to browse products, manage shopping carts, and authenticate securely using JWT, while administrators can manage the product catalog through role-based access control (RBAC). It also demonstrates backend best practices such as error handling, database seeding, and modular project organization.

User Stories

Shopper

  • As a shopper, I can register and log into my account.
  • As a shopper, I can browse all available products.
  • As a shopper, I can view product details.
  • As a shopper, I can add products to my shopping cart.
  • As a shopper, I can update product quantities in my cart.
  • As a shopper, I can remove products from my cart.
  • As a shopper, I can view my current shopping cart.

Administrator

  • As an administrator, I can create new products.
  • As an administrator, I can update existing products.
  • As an administrator, I can delete products.
  • As an administrator, I can manage the product catalog securely.

Features

Authentication

  • Register new users
  • Login existing users
  • JWT Authentication
  • Password hashing using bcryptjs

Authorization (RBAC)

  • Role-Based Access Control
  • Protected Routes
  • Admin-only Product Management
  • User-only Cart Management

Product Management

  • Get all products
  • Get product by ID
  • Create products (Admin)
  • Update products (Admin)
  • Delete products (Admin)

Advanced Query Features

  • Product Search
  • Category Filtering
  • Price Range Filtering
  • Sorting
  • Pagination

Shopping Cart

  • Automatically create a cart for new users
  • View cart
  • Add products to cart
  • Update product quantity
  • Remove products from cart
  • View a specific cart item

Other Features

  • MVC Architecture
  • Centralized Error Handling
  • Environment Variables
  • MongoDB Atlas Support
  • CORS Enabled
  • Seed Script
  • Postman Collection Included

Tech Stack

  • Node.js
  • Express.js
  • MongoDB
  • Mongoose
  • JWT
  • bcryptjs
  • dotenv
  • CORS

Project Structure

back_end/
│
├── config/
│   └── db.js
│
├── controllers/
│   ├── authController.js
│   ├── cartController.js
│   └── productController.js
│
├── middleware/
│   ├── authMiddleware.js
│   ├── adminMiddleware.js
│   └── errorHandler.js
│
├── models/
│   ├── User.js
│   ├── Product.js
│   └── Cart.js
│
├── routes/
│   ├── authRoutes.js
│   ├── cartRoutes.js
│   └── productRoutes.js
│
├── services/
│
├── utils/
│   └── generateToken.js
│
├── postman demo/
│   ├── E-Commerce.postman_collection.json
│   └── E-Commerce Environment.postman_environment.json
│
├── seed.js
├── app.js
├── index.js
├── package.json
└── README.md

API Endpoints

Authentication

Method Endpoint Description
POST /api/auth/register Register a new user
POST /api/auth/login Login
GET /api/auth/me Get current authenticated user

Products

Method Endpoint Access
GET /api/products Public
GET /api/products/:id Public
POST /api/products Admin
PATCH /api/products/:id Admin
DELETE /api/products/:id Admin

Query Parameters

Query Description
search=laptop Search products by name or description
category=Electronics Filter by category
minPrice=100 Minimum price
maxPrice=500 Maximum price
sort=price Sort ascending
sort=-price Sort descending
page=2&limit=10 Pagination

Cart

Method Endpoint
GET /api/carts
POST /api/carts
PATCH /api/carts/:productId
DELETE /api/carts/:productId
GET /api/carts/:productId

Authentication

Protected routes require a valid JWT.

Authorization: Bearer YOUR_TOKEN

Installation

Clone the repository

git clone https://github.com/Mohammed-Er/E-Commerce-API.git

Install dependencies

npm install

Create a .env file

PORT=5000
MONGO_URI=your_mongodb_uri
JWT_SECRET=your_secret

Run the project

npm run dev

or

npm start

🌱 Seed Database

Run

npm run seed

This will:

  • Delete existing Users
  • Delete existing Products
  • Delete existing Carts
  • Insert sample Users
  • Insert sample Products

Test Accounts

Admin

Email: admin@test.com
Password: 123456

User

Email: user@test.com
Password: 123456

Postman Collection

The repository includes a complete Postman Collection and Environment inside:

postman demo/

Import both files into Postman.

Environment Variables:

  • baseUrl
  • jwt
  • productId

Use an Admin JWT for product management endpoints.

Use a User JWT for shopping cart endpoints.


Future Improvements

  • Order Management System
  • Wishlist
  • Product Reviews
  • Checkout System
  • Stripe Integration
  • React Frontend
  • Docker Support
  • Swagger API Documentation

About

RESTful E-commerce Backend API built with Node.js, Express, MongoDB, JWT Authentication, and MVC Architecture.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages