Skip to content

SuvojitDev/reverseProxy

Repository files navigation

🔁 Reverse Proxy Server (Node.js + Express + MongoDB)

A lightweight reverse proxy built using Node.js, Express, and MongoDB, featuring:

✅ Route-based proxying 📄 Request logging to MongoDB 🔒 IP-based rate limiting 📈 Health and Prometheus-style metrics endpoints 🛠 Minimal dependencies, no heavy frameworks


📁 Project Structure

├── db/
│   └── mongoClient.js        # MongoDB connection + collection handler
├── middleware/
│   ├── rateLimiter.js        # Rate limiting logic (express-rate-limit)
│   └── requestLogger.js      # Logs requests to MongoDB
├── proxy/
│   ├── routeConfig.js        # Maps prefixes to backend services
│   └── router.js             # Handles proxying logic using native http
├── mock-service1.js          # Mock service on port 4000 (for testing)
├── mock-service2.js          # Mock service on port 5000 (for testing)
├── server.js                 # Main app entry point
├── .env                      # Environment config
├── package.json
└── README.md

🚀 Features

🔁 Reverse Proxying

Routes incoming requests based on prefix:

Proxy Path Target Backend
/api/service1/* http://localhost:4000
/api/service2/* http://localhost:5000

📝 Request Logging (MongoDB)

Every request is logged with:

  • Method, URL, headers
  • Status code and response time
  • Timestamp

Stored in: proxyLogs.requests collection


🛡️ Rate Limiting

Limits each IP to 30 requests per minute using express-rate-limit.

After exceeding:

{
  "status": 429,
  "error": "Too many requests, please try again later."
}

🔍 Health Check

GET /health

Returns app and DB status, uptime, memory usage:

{
  "status": "ok",
  "db": "connected",
  "uptime": "123s",
  "memory": 34506752,
  "version": "1.0.0",
  "hostname": "localhost"
}

📊 Metrics Endpoint (Prometheus-style)

GET /metrics

Returns Prometheus-compatible plain text metrics like:

# HELP app_uptime_seconds Uptime in seconds
app_uptime_seconds 123.45
# HELP app_memory_rss_bytes Resident memory usage
app_memory_rss_bytes 34506752

🧪 Testing

✅ Proxy Test

Use Postman or curl:

  • GET http://localhost:3000/api/service1/hello → "Hello from Service 1"
  • GET http://localhost:3000/api/service2/hello → "Hello from Service 2"

🔁 Trigger Rate Limit

for i in {1..12}; do
  curl -s -o /dev/null -w "%{http_code}\n" http://localhost:3000/api/service1/hello
done

Last 2 requests should return 429.


⚙️ Setup

1. Clone + Install

git clone <your-repo-url>
cd reverse-proxy
npm install

2. Configure .env

PORT=3000
MONGO_URI=mongodb+srv://<user>:<password>@<cluster-url>/reverseProxy

3. Start the Server

npm start

Also start the mock services:

node mock-service1.js
node mock-service2.js

🧠 Future Improvements (Optional)

  • JWT/Auth support
  • Redis-based rate limiting
  • HTTPS & TLS termination
  • Admin UI to view logs
  • Load balancing between targets

About

Lightweight reverse proxy with Express, MongoDB logging, rate limiting, and Prometheus metrics.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published