Skip to content

Mohammed-Er/E-Commerce-API

Repository files navigation

🛒 E-Commerce REST API

A secure RESTful E-Commerce backend built with Node.js, Express.js, and MongoDB following the MVC Architecture. The API implements authentication, role-based authorization (RBAC), product management, and shopping cart functionality.


Features

Authentication

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

Authorization

  • Role-Based Access Control (RBAC)
  • Protected routes
  • Admin-only product management

Product Management

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

Shopping Cart

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

Other

  • 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
│
├── 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 user
POST /api/auth/login Login
GET /api/auth/me Get current 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

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 JWT token.

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 server

npm run dev

or

npm start

🌱 Seed Database

The project includes a seed script that clears the database and inserts sample users and products for testing.

Run:

npm run seed

This will:

  • Delete existing Users
  • Delete existing Products
  • Delete existing Carts
  • Insert sample Admin and User accounts
  • Insert sample Products

📬 Postman Collection

A complete Postman Collection and Environment are included inside:

postman demo/

Import both files into Postman to test every endpoint immediately.

The environment already contains variables such as:

  • baseUrl
  • jwt
  • productId

Simply login once to generate a JWT and paste it into the jwt variable. Note that some operations requires an admin jwt token like adding a new product and some operations like cart endpoints requires user jwt token


📈 Future Improvements

  • Order System
  • Product Search
  • Filtering
  • Sorting
  • Pagination
  • Wishlist
  • Product Reviews
  • Checkout
  • Stripe Integration
  • React Frontend

About

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

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Packages

 
 
 

Contributors