How nitinpratap22061/reverseproxy-server works
Project Type and Purpose
• This is a middleware application acting as a reverse proxy. It intercepts incoming client requests, forwards them to one of the configured backend servers (backend1.js and backend2.js), caches responses for performance, and handles errors such as when a backend server is unavailable (returning a 502 error).
Key Components and Their Roles
• Proxy Server (server.js): Acts as the central gateway listening on port 8000. It handles incoming HTTP requests, applies load balancing, manages caching using NodeCache, and forwards requests to backend servers.
• Backend Servers (backend1.js and backend2.js): These represent the service instances that actually process the forwarded requests. They are run on different ports (as defined in the .env file).
• Load Balancing Component: Implemented within the proxy server using a round-robin algorithm. It decides which backend to forward each request.
• Caching Component: Utilizes NodeCache to store responses temporarily, reducing the load on backend servers and speeding up client responses.
• Environment Configuration: Uses environment variables (as mentioned in the README) for dynamic configuration (e.g., backend hosts/ports, cache TTL).
Relationships and Interactions
• The client initiates a request to the proxy server.
• The proxy server examines the request and consults its load balancing logic to select either backend1.js or backend2.js.
• The proxy then checks if a cached response exists:
• If yes, it returns the cached response.
• If not, it forwards the request to the selected backend server.
• The backend processes the request and sends the response back. The proxy caches this response before returning it to the client.
• Error handling is applied; if a selected backend server is unreachable, the proxy returns a “502 Bad Gateway” error.
Architectural Patterns and Design Principles
• Reverse Proxy Pattern: Central server forwards client requests to appropriate backend services.
• Load Balancer: Round-robin algorithm implemented within the proxy.
• Caching Mechanism: NodeCache is used to reduce redundancy and improve performance.
• Separation of Concerns: The proxy’s responsibilities (routing, caching, error handling) are distinct from the business logic in the backend servers.