Skip to content

Latest commit

Β 

History

290 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›’ eCommerce Backend API

CodeQL Node >=22 License: ISC

A Node.js / Express REST API for an e-commerce backend. It provides authentication, role-based access control, and management for categories, products, shopping carts, and user accounts (registration plus admin-only listing) β€” backed by MySQL (via Sequelize ORM) with Redis integration.


πŸ“š Table of Contents


✨ Features

  • JWT Authentication β€” sign-up, login, logout, and password change with signed access tokens.
  • Role-Based Access Control β€” Customer, Admin, and SuperAdmin roles enforced via middleware.
  • User Management β€” registration with automatic cart creation and admin-only user listing.
  • Category Management β€” full CRUD (admin restricted for write operations).
  • Product Management β€” create, read, update, delete, filter, and paginated listing.
  • Shopping Cart β€” update and retrieve a cart by id.
  • Redis Integration β€” a Redis client writes user data to the store on login.
  • Input Validation β€” request payloads validated with Joi.
  • Environment Validation β€” strongly typed config via Envalid.
  • Auto Database Bootstrap β€” the database and default admin are created/seeded on startup.
  • Structured Logging & Security β€” morgan HTTP logging, CORS, and cookie parsing.

🧰 Tech Stack

Layer Technology
Runtime Node.js (>= 22, ESM modules)
Framework Express 5
ORM / DB Sequelize 6 + MySQL (mysql2)
Cache / Redis redis client (user data written on login)
Auth JSON Web Tokens (jsonwebtoken), bcrypt hashing
Validation Joi, Envalid
Middleware cors, cookie-parser, morgan
Testing Jest, Supertest
Formatting Prettier

πŸ“ Project Structure

ecommerce/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ index.js              # Application entry point (DB sync + bootstrap + listen)
β”‚   β”œβ”€β”€ app.js                # Express app, middleware, route mounting
β”‚   β”œβ”€β”€ constants.js
β”‚   β”œβ”€β”€ configs/              # Database & Sequelize configuration
β”‚   β”œβ”€β”€ controllers/          # Request handlers (auth, product, category, cart)
β”‚   β”œβ”€β”€ middlewares/          # JWT auth, role checks, validation
β”‚   β”œβ”€β”€ models/               # Sequelize models (User, Role, Category, Product, Cart)
β”‚   β”œβ”€β”€ routes/               # Express routers
β”‚   └── utils/                # Env, redis, response, helpers, validation, cron
β”œβ”€β”€ tests/                    # Jest test suites (controllers, routes)
β”œβ”€β”€ .env / .env.sample        # Environment configuration
└── package.json

βœ… Prerequisites

  • Node.js >= 22
  • MySQL server (default port 3306)
  • Redis server (default redis://localhost:6379)
  • npm (bundled with Node.js)

πŸ›  Installation

# 1. Clone the repository
git clone https://github.com/nil2022/ecommerce.git
cd ecommerce

# 2. Install dependencies
npm install

# 3. Configure environment variables
cp .env.sample .env
# then edit .env with your database, redis, and secret values

πŸ”§ Environment Configuration

Copy .env.sample to .env and adjust the values. All variables are validated at startup by Envalid, so a missing/invalid value will fail fast.

Variable Default Description
PORT 9000 Port the server listens on
NODE_ENV dev Environment (dev, prod, test)
CORS_ORIGIN (empty) Allowed CORS origin
CORS_ALLOWED_HEADERS (empty) Comma-separated allowed headers
ACCESS_TOKEN_SECRET secret Secret used to sign JWTs
ACCESS_TOKEN_EXPIRY 7d Access token expiry duration
DB_HOST localhost MySQL host
DB_USER root MySQL username
DB_PASSWORD root MySQL password
DB_NAME ecomm MySQL database name
DB_PORT 3306 MySQL port
DB_SSL false Use SSL for the database connection
REDIS_URL redis://localhost:6379 Redis connection URL
SYSTEM_ADMIN_USERID john System/admin bootstrap user ID
SYSTEM_ADMIN_PASSWORD 12345678 System/admin bootstrap password
SYSTEM_ADMIN_EMAIL john@email.com System/admin bootstrap email

πŸ—„ Database Setup

No manual migrations are required. On startup the application will:

  1. Create the database (DB_NAME) if it does not already exist (using the configured DB host/port).
  2. Authenticate the Sequelize connection.
  3. Synchronize all models (creating tables that do not exist β€” non-destructive; runs on every startup).
  4. Seed default data via initialize() only when a given table is empty:
    • Roles: Customer, Admin, SuperAdmin
    • Category: Beauty
    • Product: MakeUP Kit (cost 870, quantity 20, under the Beauty category)
    • System Administrator: created from SYSTEM_ADMIN_USERID / SYSTEM_ADMIN_PASSWORD / SYSTEM_ADMIN_EMAIL, assigned the SuperAdmin role, with an associated cart.
# Start the server (this triggers the bootstrap above)
npm start

πŸš€ Running the Application

# Development mode (hot reload via Node --watch)
npm run dev

# Production start
npm start

# Run the test suite
npm test

# Run tests with coverage
npm run test:cov

The server will be available at http://localhost:<PORT> (default 9000).


πŸ“‘ API Reference

All routes are mounted under the following base paths:

  • /auth
  • /category
  • /product
  • /cart

Authentication

Most endpoints require a valid access token. The token is read from (in order of precedence):

  • The accessToken cookie, or
  • The Authorization: Bearer <token> header, or
  • The x-access-token header.

Admin-only endpoints additionally require the authenticated user to hold the Admin or SuperAdmin role.


Auth Routes β€” /auth

Method Endpoint Auth Admin Description
POST /auth/register Register a new user (auto-creates a cart).
POST /auth/login Authenticate and receive an access token.
GET /auth/all-users βœ… Super List all users (SuperAdmin only).
PATCH /auth/change-password βœ… Change the logged-in user's password.
GET /auth/logout βœ… Clear the auth cookie / log out.

Register body

{
  "fullName": "John Doe",
  "userId": "john",
  "email": "john@example.com",
  "password": "secret123",
  "roles": [1]
}

Login body

{ "userId": "john", "password": "secret123" }

Category Routes β€” /category

Method Endpoint Auth Admin Description
POST /category/add βœ… βœ… Create a new category.
GET /category/getAll βœ… List all categories.
GET /category/getOne βœ… Get a category by query id.
PATCH /category/updateOne βœ… βœ… Update a category.
DELETE /category/deleteOne βœ… βœ… Delete a category.

Product Routes β€” /product

Method Endpoint Auth Admin Description
POST /product/add βœ… βœ… Create a new product.
GET /product/getAll βœ… List all products.
GET /product/filter βœ… Filter products by query params.
GET /product/getOne βœ… Get a product by query id.
PATCH /product/updateOne βœ… βœ… Update a product.
DELETE /product/deleteOne βœ… βœ… Delete a product.

Cart Routes β€” /cart

Method Endpoint Auth Admin Description
PUT /cart/:id βœ… Update the cart for user :id.
GET /cart/:id βœ… Retrieve the cart for user :id.

⚠️ Authorization note: These routes only require a valid token; they do not verify that the authenticated user owns the cart identified by :id. Any authenticated user can currently read or modify another user's cart (insecure direct object reference). Enforcing ownership (or admin access) is recommended before production use.


πŸ“¦ Project Scripts

Script Command Purpose
dev node --env-file=.env --watch src/index.js Run in development with hot reload.
start node --env-file=.env src/index.js Run in production.
test jest --verbose --runInBand --detectOpenHandles --forceExit Run the Jest test suite.
test:cov cross-env NODE_ENV=test jest --coverage Run tests with coverage reporting.

🀝 Contributing

Contributions are welcome! If you find any issues or have suggestions for improvement, please open an issue or submit a pull request.

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature/my-feature).
  3. Commit your changes (git commit -m 'Add my feature').
  4. Push to the branch (git push origin feature/my-feature).
  5. Open a pull request.

πŸ“„ License

This project is licensed under the ISC License.

About

An E-commerce web app built with Node.js and using MySQL/PostgreSQL as database.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages