Skip to content

AdzKalnain/E-Commerce-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🛒 E-Commerce API

A RESTful API built with Node.js, Express, and MongoDB (via Mongoose) that serves as the backend for a fully functional e-commerce platform. Developed during my time at Zuitt Coding Bootcamp (2022).


📖 Overview

This API handles the core operations of an e-commerce system — from user registration and authentication to product management and order processing. It features a role-based access control system using JSON Web Tokens (JWT), ensuring that only authorized users and admins can perform sensitive actions.


✨ Features

  • User Management — Register, log in, promote users to admin, and retrieve profile details
  • Product Management — Admins can add, update, archive, and reactivate products; public users can browse active listings
  • Order Management — Authenticated users can place orders, view their order history, and inspect individual orders; admins can view all orders across users
  • Role-Based Access Control — Protected routes using JWT middleware, with additional admin-only guards on sensitive endpoints
  • Secure Authentication — Passwords are hashed with bcrypt; sessions are managed via signed JWTs
  • Duplicate Email Prevention — Registration checks for existing emails before creating a new account

🛠️ Tech Stack

Layer Technology
Runtime Node.js
Framework Express.js
Database MongoDB Atlas
ODM Mongoose
Authentication JSON Web Tokens (jsonwebtoken)
Password Hashing bcrypt

🗂️ Data Models

User

User
├── firstName       String (required)
├── lastName        String (required)
├── mobileNo        String (required)
├── email           String (required)
├── password        String (required, hashed)
└── isAdmin         Boolean (default: false)

Product

Product
├── name            String (required)
├── description     String (required)
├── price           Number (required)
├── isActive        Boolean (default: true)
└── createdOn       Date

Order

Order
├── totalAmount     Number (required)
├── purchasedOn     Date
├── userId          String (required)
└── products[]
    ├── productId   String (required)
    └── quantity    Number (default: 1)

🔌 API Endpoints

👤 User Routes — /users

Method Endpoint Access Description
POST /users Public Register a new user
POST /users/login Public Log in and receive a JWT
GET /users/getUserDetails Authenticated Get logged-in user's profile
PUT /users/setAdmin/:userId Admin Promote a user to admin

📦 Product Routes — /products

Method Endpoint Access Description
POST /products/insertProduct Admin Add a new product
GET /products/activeProducts Public Get all active products
GET /products/getProduct/:productId Public Get a single product by ID
PUT /products/updateProduct/:productId Admin Update a product
PUT /products/activateProduct/:productId Admin Reactivate an archived product
DELETE /products/archiveProduct/:productId Admin Archive (soft delete) a product

🧾 Order Routes — /orders

Method Endpoint Access Description
POST /orders Authenticated Place a new order
GET /orders/getUserOrders Authenticated Get all orders of the logged-in user
GET /orders Admin Get all orders from all users
GET /orders/productPerOrder/:orderId Authenticated Get products within a specific order

🔐 Authentication

This API uses Bearer Token Authorization via JWT. To access protected routes, include the token in the request header:

Authorization: Bearer <your_token_here>

Tokens are issued upon successful login and contain the user's id, email, and isAdmin status.


🚀 Getting Started

Prerequisites

  • Node.js
  • MongoDB Atlas account

Installation

  1. Clone the repository

    git clone https://github.com/AdzKalnain/ecommerce-api.git
    cd ecommerce-api
  2. Install dependencies

    npm install
  3. Configure your MongoDB connection

    In index.js, update the connection string with your own MongoDB Atlas credentials:

    mongoose.connect("your-mongodb-connection-string", { ... });
  4. Start the server

    node index.js

    The server will run at http://localhost:4000


📁 Project Structure

├── index.js                    # Entry point, DB connection, and middleware setup
├── auth.js                     # JWT creation and verification middleware
├── models/
│   ├── User.js                 # Mongoose User schema and model
│   ├── Product.js              # Mongoose Product schema and model
│   └── Order.js                # Mongoose Order schema and model
├── controllers/
│   ├── userControllers.js      # Business logic for user routes
│   ├── productControllers.js   # Business logic for product routes
│   └── orderControllers.js     # Business logic for order routes
└── routes/
    ├── userRoutes.js           # User route definitions
    ├── productRoutes.js        # Product route definitions
    └── orderRoutes.js          # Order route definitions

🧪 Sample Product Listings

Product Description Price (PHP)
Nike Air Max SC Men's Shoes Real and synthetic leather with breathable mesh ₱3,895
Under Armour UA Spawn 4 Lightweight engineered mesh basketball shoes ₱5,595
Anta Attack 4 Basketball Shoes Multi-tone mesh panelled chunky basketball shoes ₱4,695
World Balance Collider Comfort-focused lifestyle sneakers ₱1,999
Converse Chuck 70 Hi Sneakers Graphic print high-top sneakers ₱5,149

👨‍💻 Author

Developed as part of the Zuitt Coding Bootcamp (2022).

About

A RESTful API built with Node.js, Express, and MongoDB (via Mongoose) that serves as the backend for a fully functional e-commerce platform. Developed during my time at Zuitt Coding Bootcamp (2022).

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors