Skip to content

Rohitsinghwho/Proxy-Server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Proxy Server

A high-performance HTTP/HTTPS proxy server built with Node.js and TypeScript. This proxy forwards requests to an origin server and caches GET responses in memory for improved performance.

Features

  • HTTP/HTTPS Support — Handles both HTTP and HTTPS protocols
  • Request Forwarding — Proxies all HTTP methods (GET, POST, PUT, DELETE, etc.)
  • In-Memory Caching — Caches GET requests with TTL expiration
  • Streaming — Efficient memory usage with request/response streams
  • Error Handling — Graceful error handling and reporting
  • TypeScript — Fully typed for safety and IDE support

Installation

npm install

Build

npm run build

Usage

Start the proxy server

npm start -- --port <PORT> --origin <ORIGIN_URL>

Development mode (with auto-reload)

npm run dev -- --port <PORT> --origin <ORIGIN_URL>

Examples

Forward to an HTTP API:

npm start -- --port 3000 --origin http://api.example.com

Forward to an HTTPS API:

npm start -- --port 3000 --origin https://api.example.com

Forward with a path:

npm start -- --port 3000 --origin https://api.example.com/v1

Then make requests to the proxy:

curl http://localhost:3000/products
curl http://localhost:3000/users -X POST -d '{"name":"John"}'

How It Works

Request Flow

  1. Client Request → Proxy receives request
  2. Cache Check → If GET request is cached and not expired, return cached response
  3. Cache Miss → Forward request to origin server
  4. Buffer Response → Collect response body from origin
  5. Cache Store → Store response in memory (GET requests only)
  6. Send Response → Send response back to client with x-cache: HIT or x-cache: MISS header

Streaming

  • Upload Stream (req.pipe(forwardReq)) — Client request body → Origin server
  • Download Stream (originRes.pipe(res)) — Origin response body → Client

This ensures memory-efficient handling of large files and responses.

Caching Strategy

  • What's Cached — Only GET requests
  • TTL — Default 5 minutes expiration
  • Memory Limit — 50MB max cache size
  • Headers — Response headers are preserved, x-cache header added

Configuration

Command-Line Options

Option Description Example
--port Port to listen on --port 3000
--origin Origin server URL to proxy to --origin http://api.example.com

Both options are required.

API Response Headers

Header Value Meaning
x-cache HIT Response served from cache
x-cache MISS Response from origin server

Project Structure

proxy-server/
├── src/
│   └── index.ts          # Main proxy server implementation
├── dist/                 # Compiled JavaScript (after build)
├── package.json          # Node.js dependencies and scripts
├── tsconfig.json         # TypeScript configuration
└── README.md            # This file

Development

Scripts

npm run build    # Compile TypeScript to JavaScript
npm start        # Run the compiled proxy server
npm run dev      # Run with TypeScript directly (auto-reload)

Requirements

  • Node.js 18+
  • npm or yarn

Error Handling

The proxy handles common errors gracefully:

  • Port Already In Use — Exits with error message
  • Invalid Origin — Exits with error message
  • Connection Errors — Returns 500 error to client
  • Missing Arguments — Exits with usage instructions

Performance Considerations

  • Streaming — Uses Node.js streams to avoid loading entire responses into memory
  • Caching — Reduces latency for repeated GET requests
  • TTL Expiration — Prevents stale data accumulation
  • Backpressure Handling — Automatic flow control via streams

Example Use Cases

  1. API Gateway — Proxy to multiple backend services
  2. Rate Limiting — Combine with rate limiting middleware
  3. Request Logging — Add logging for all requests
  4. CDN Alternative — Cache and serve frequently accessed resources
  5. Development — Proxy to remote APIs during local development

License

ISC

Author

Rohit Singh

About

A high-performance HTTP/HTTPS proxy server built with Node.js and TypeScript. This proxy forwards requests to an origin server and caches GET responses in memory for improved performance.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors