This project is a Node.js E-Commerce API built with Express.js, API Gateway, and multiple microservices (Auth Service, Product Service, and Order Service). The system uses MongoDB, Redis Cache, and Docker Compose for seamless development and deployment.
- Microservices Architecture
- API Gateway using Express.js
- Authentication & Authorization
- Product Management
- Order Processing
- MongoDB for database storage
- Redis for caching
- Docker Compose for easy containerized setup
- Development & Production Configurations
Below is the architecture representation of the system:
┌──────────────────────┐
│ Client (Frontend) │
└────────▲────────────┘
│
▼
┌───────────────────┐
│ API Gateway │ (Express.js)
└────────▲──────────┘
│
┌──────────────┴──────────────┐
│ │
▼ ▼
┌──────────────┐ ┌──────────────┐
│ Auth Service │ │ Order Service│
│ (Node.js) │ │ (Node.js) │
└──────────────┘ └──────────────┘
│ │
▼ ▼
┌──────────────┐ ┌──────────────┐
│ MongoDB (Auth)│ │ MongoDB (Order)│
└──────────────┘ └──────────────┘
┌──────────────┐ ┌──────────────┐
│ Product Service │ │ Redis Cache │
│ (Node.js) │ │ (Session Cache) │
└──────────────┘ └──────────────┘
│
▼
┌──────────────┐
│ MongoDB (Product)│
└──────────────┘
- Routes requests to respective microservices.
- Manages authentication & request validation.
- User authentication & authorization.
- Token-based authentication (JWT).
- Manages user data in MongoDB.
- Handles product listing & inventory.
- CRUD operations on products.
- Manages order placement, processing, and tracking.
- Interacts with Product Service and Auth Service.
To start the development environment, use Docker Compose:
docker-compose up --buildFor production, we use two separate Docker Compose files:
docker-compose -f docker-compose-production.yml up --build- Node.js (Backend Framework)
- Express.js (API Gateway & Microservices)
- MongoDB (Database for Auth, Product, and Order services)
- Redis (Cache for session storage & optimization)
- Docker & Docker Compose (Containerization)
- JWT (Authentication)
- Fork the repository
- Clone your fork
git clone https://github.com/your-username/nodejs-ecommerce-microservices-api-gateway.git
- Create a feature branch
git checkout -b feature-new-feature
- Make changes & commit
git commit -m "Added new feature" - Push changes & create PR
git push origin feature-new-feature
This guide provides step-by-step instructions to deploy a Node.js CRUD API with MongoDB using Docker and Docker-Compose on an AWS EC2 instance.
- Go to AWS Console → EC2 → Launch Instance.
- Select Ubuntu 22.04 as the OS.
- Choose an instance type:
- t2.micro (Free Tier) for testing.
- t3.small or t3.medium for production.
- Create and Download a Key Pair (if not already created).
- Configure Security Group (see next step).
- Click Launch Instance.
- Open EC2 → Security Groups.
- Edit Inbound Rules and add the following:
- SSH → TCP → 22 → Source:
0.0.0.0/0 - HTTP → TCP → 80 → Source:
0.0.0.0/0 - HTTPS → TCP → 443 → Source:
0.0.0.0/0 - Custom TCP → TCP → 6767 (Your API Port) → Source:
0.0.0.0/0
- SSH → TCP → 22 → Source:
Use the .pem key to connect:
ssh -i "C:\Users\YourUserName\Downloads\nodejs.pem" ubuntu@your-ec2-public-ipOn your local machine, run:
scp -i "C:\Users\YourUserName\Downloads\nodejs.pem" -r "D:\DSA\node-authentication-project1" ubuntu@your-ec2-public-ip:/home/ubuntu/Now, your project is in /home/ubuntu/node-authentication-project1 on EC2.
Run these commands on EC2:
# Update packages
sudo apt update && sudo apt upgrade -y
# Install Docker
sudo apt install docker.io -y
# Start and Enable Docker
sudo systemctl start docker
sudo systemctl enable docker
# Add Ubuntu user to Docker group
sudo usermod -aG docker ubuntu
# Install Docker-Compose
sudo curl -L "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/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-composeVerify installation:
docker --version
docker-compose --versionInside your project folder (/home/ubuntu/node-authentication-project1), create a Dockerfile:
FROM node:18
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 6767
CMD ["node", "index.js"]Create docker-compose.yml in the project root:
version: '3.8'
services:
auth-service:
build:
context: ./
dockerfile: Dockerfile
ports:
- "6767:6767"
depends_on:
- mongodb
environment:
MONGODB_URI: "mongodb+srv://<db_username>:<db_password>@<cluster_name>/<table_name>"
SECRET_KEY: "my_secret_key"
networks:
- microservices-net
mongodb:
image: mongo
container_name: mongodb
ports:
- "27018:27017"
volumes:
- mongo-data:/data/db
networks:
- microservices-net
volumes:
mongo-data:
networks:
microservices-net:
external: trueRun this command to create the Docker network before running docker-compose:
docker network create microservices-netNavigate to your project directory on EC2:
cd /home/ubuntu/node-authentication-project1Run the Docker containers:
docker-compose up --build -dCheck running containers:
docker psOnce the containers are running, access your API using:
http://your-ec2-public-ip:6767/api/admin/usersIf you want to use a custom domain with HTTPS, set up Nginx as a reverse proxy.
-
Install Nginx:
sudo apt install nginx -y
-
Configure Nginx:
sudo nano /etc/nginx/sites-available/default
-
Replace the content with:
server { listen 80; server_name your-domain.com; location / { proxy_pass http://localhost:6767; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
-
Restart Nginx:
sudo systemctl restart nginx
Now, access your API at:
http://your-domain.com/api/admin/usersTo restart the containers automatically, run:
docker-compose restartTo pull new updates and redeploy:
git pull origin main
docker-compose up --build -dThis project is licensed under the MIT License.