A production-style API Gateway built with Node.js that acts as the single entry point for a microservices-based backend system. It handles rate limiting, response caching, JWT authentication, and structured logging β routing traffic intelligently to three independent downstream microservices, each backed by its own isolated MongoDB instance.
Client
β
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββ
β API Gateway (Port 3000) β
β β
β ββββββββββββ ββββββββββ βββββββββββββββ β
β β Rate β β Redis β β JWT Auth β β
β β Limiter β β Cache β β Middleware β β
β ββββββββββββ ββββββββββ βββββββββββββββ β
β ββββββββββββββββββββββββββββββββββββββββββ β
β β Morgan HTTP Logger β β
β ββββββββββββββββββββββββββββββββββββββββββ β
β ββββββββββββββββββββββββββββββββββββββββββ β
β β Request Router (routes.js) β β
β ββββββββββββ¬βββββββββββ¬βββββββββββββββββββ β
βββββββββββββββΌβββββββββββΌβββββββββββββββββββββββ
β β β
βββββββββββ βββββββββ ββββββββ
βΌ βΌ βΌ
User Service Product Order
(Port 4000) Service Service
(MongoDB) (Port 5001)(Port 6000)
(MongoDB) (MongoDB)
β
Prometheus (Port 9090)
Grafana (Port 3001)
All services communicate over a shared Docker bridge network (mynetwork) and are orchestrated via Docker Compose.
| Feature | Details |
|---|---|
| API Gateway | Single entry point routing to all microservices |
| Rate Limiting | Redis-backed, 1000 requests/60 seconds per IP |
| Response Caching | Redis cache with 60-second TTL on all GET requests |
| JWT Authentication | Token-based auth middleware (pluggable) |
| Structured Logging | HTTP access logs via Morgan written to access.log |
| API Documentation | Swagger UI served at /docs |
| Containerised | All services Dockerized, orchestrated with Docker Compose |
| Monitoring | Prometheus metrics scraping + Grafana dashboards |
| Input Validation | express-validator on sensitive endpoints |
api-gateway/
βββ gateway/ # API Gateway service
β βββ server.js # Express app bootstrap & middleware registration
β βββ routes.js # Request forwarding logic to microservices
β βββ auth.js # JWT authentication middleware
β βββ rateLimiter.js # Redis-backed rate limiting middleware
β βββ cache.js # Redis response caching middleware
β βββ logging.js # Morgan HTTP access logger
β βββ swagger.js # Swagger/OpenAPI documentation setup
β βββ Dockerfile # Gateway container definition
β βββ package.json
β
βββ microservices/
β βββ user-service/ # User management microservice (Port 4000)
β β βββ server.js # Register, Login, Profile endpoints
β β βββ Dockerfile
β β βββ .env # AUTH_KEY secret
β β βββ package.json
β β
β βββ product-service/ # Product catalog microservice (Port 5001)
β β βββ server.js # Create, List, Update Stock endpoints
β β βββ Dockerfile
β β βββ package.json
β β
β βββ order-service/ # Order management microservice (Port 6000)
β βββ server.js # Create Order, Get Orders by User endpoints
β βββ Dockerfile
β βββ package.json
β
βββ monitoring/
β βββ prometheus.yml # Prometheus scrape config (15s interval)
β βββ grafana/ # Grafana dashboard config
β
βββ deployment/
βββ docker-compose.yml # Full stack orchestration
| Technology | Version | Purpose |
|---|---|---|
| Node.js | 18 | Runtime for all services |
| Express.js | ^4.21.0 | HTTP server framework |
| Package | Purpose |
|---|---|
ioredis |
Redis client for rate limiting & caching |
rate-limiter-flexible |
Redis-backed rate limiting strategy |
jsonwebtoken |
JWT generation & verification |
morgan |
HTTP request logger |
node-fetch |
Internal HTTP forwarding to microservices |
swagger-jsdoc + swagger-ui-express |
OpenAPI 2.0 documentation |
| Package | Purpose |
|---|---|
mongoose |
MongoDB ODM for all three services |
bcryptjs |
Password hashing (User Service) |
express-validator |
Input validation (User & Product services) |
dotenv |
Environment variable management |
| Technology | Purpose |
|---|---|
| Docker | Containerisation of each service |
| Docker Compose | Multi-container orchestration |
| Redis | Rate limiting store + response cache |
| MongoDB | Document database (3 isolated instances) |
| Prometheus | Metrics collection (scrapes gateway every 15s) |
| Grafana | Metrics visualisation dashboard |
All client requests hit the API Gateway at port 3000, which proxies them to the appropriate microservice.
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
POST |
/api/users/register |
Register a new user | β |
POST |
/api/users/login |
Login and receive JWT token | β |
GET |
/api/users/me |
Get authenticated user profile | β |
Register Request Body:
{
"name": "John Doe",
"email": "john@example.com",
"password": "secret123"
}Login Response:
{
"token": "<JWT>"
}| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
POST |
/api/products |
Create a new product | β |
GET |
/api/products |
List all products | β |
PUT |
/api/products/:id/stock |
Update product stock level | β |
Create Product Request Body:
{
"name": "Widget",
"price": 9.99,
"stock": 100
}| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
POST |
/api/orders |
Create a new order (auto-calculates total) | β |
GET |
/api/orders/user/:userId |
Get all orders for a specific user | β |
Create Order Request Body:
{
"userId": "<user_id>",
"products": [
{ "productId": "<product_id>", "quantity": 2 }
]
}Note: The Order Service fetches real-time product prices from the Product Service internally to compute
totalAmount.
Requests entering the gateway pass through the following middleware stack in order:
Incoming Request
β
βΌ
1. Morgan Logger β Appends to access.log
β
βΌ
2. Rate Limiter β 1000 req/min per IP via Redis
β Returns 429 if exceeded
βΌ
3. Redis Cache β Checks Redis for cached response (GET)
β Returns cached JSON if hit (200)
βΌ
4. Router (routes.js) β Forwards request to correct microservice
β via node-fetch (method + headers preserved)
βΌ
Microservice Response β Cached in Redis (TTL: 60s), returned to client
JWT Auth middleware is implemented in
auth.jsand can be enabled by uncommenting theapp.use('/api/*', auth)line inserver.js.
- Scrapes the API Gateway at
gateway:3000every 15 seconds - Accessible at http://localhost:9090
- Pre-configured container based on Prometheus data source
- Build custom dashboards to visualise request rates, latency, and error rates
- Docker & Docker Compose installed
# Clone the repository
git clone <repo-url>
cd api-gateway
# Start all services
cd deployment
docker-compose up --build| Service | URL |
|---|---|
| API Gateway | http://localhost:3000 |
| Swagger Docs | http://localhost:3000/docs |
| User Service | http://localhost:4000 |
| Product Service | http://localhost:5001 |
| Order Service | http://localhost:6000 |
| Prometheus | http://localhost:9090 |
| MongoDB (User) | localhost:27017 |
| MongoDB (Product) | localhost:27018 |
| MongoDB (Order) | localhost:27019 |
| Redis | localhost:6379 |
AUTH_KEY=your_jwt_secret_key
PORT=4000{
name: String,
email: String (unique),
password: String (bcrypt hashed)
}{
name: String,
price: Number,
stock: Number
}{
userId: String,
products: [{ productId: String, quantity: Number }],
totalAmount: Number,
status: String (default: "Pending")
}This project is open source and available under the MIT License.