A simple reverse proxy server with configurable routing and CORS support.
npm install -g flexible-reverse-proxy
npm install flexible-reverse-proxy
After global installation, you can use the flexible-proxy
command:
# Basic usage with routes
flexible-proxy --route "/api:http://localhost:3000" --route "/auth:https://auth.example.com"
# Custom port
flexible-proxy --port 3000 --route "/api:http://localhost:3000"
# Custom host and port
flexible-proxy --host 0.0.0.0 --port 8080 --route "/api:http://localhost:3000"
# Load routes from file
flexible-proxy --routes-file routes.json
# Watch routes file for changes and auto-reload
flexible-proxy --routes-file routes.json --watch
# Enable verbose logging
flexible-proxy --route "/api:http://localhost:3000" --verbose
# Different log levels
flexible-proxy --route "/api:http://localhost:3000" --log-level detailed
flexible-proxy --route "/api:http://localhost:3000" --log-level full
# Control header forwarding
flexible-proxy --route "/api:http://localhost:3000" --no-change-origin
flexible-proxy --route "/api:http://localhost:3000" --no-preserve-headers
# Show help
flexible-proxy --help
-
-p, --port <port>
- Port to listen on (default: 8000) -
-h, --host <host>
- Host to bind to (default: localhost) -
-r, --route <pattern:target>
- Add a route (pattern:target). Can be used multiple times -
--routes-file <file>
- Load routes from a JSON file -
--watch
- Watch routes file for changes and auto-reload -
--verbose
- Enable verbose logging -
--log-level <level>
- Log level: basic, detailed, full (default: basic) -
--preserve-headers
- Preserve all original headers (default: true) -
--no-preserve-headers
- Do not preserve original headers -
--change-origin
- Change the origin header to target host (default: true) -
--no-change-origin
- Do not change the origin header -
--version
- Show version -
--help
- Show help
The proxy now features beautiful, colored logging with emojis and structured output:
🚀 Flexible Reverse Proxy Server
══════════════════════════════════════════════════
📍 Server running at http://localhost:8000
📊 Log level: detailed
🛣️ Routes configured:
/api → http://localhost:3000
/auth → https://auth.example.com
══════════════════════════════════════════════════
[2024-01-15T10:30:45.123Z] [abc123def456]
GET /api/users → http://localhost:3000
📋 Headers:
Authorization: Bearer token123
Content-Type: application/json
📦 Body: {"name": "John"}
[2024-01-15T10:30:45.125Z] [abc123def456]
200 OK
📋 Response Headers:
Content-Type: application/json
📦 Response Body: {"users": [...]}
[2024-01-15T10:30:45.125Z] [abc123def456]
❌ Proxy error: connect ECONNREFUSED
The proxy server forwards all headers to the target server by default. Here's how header handling works:
- All headers are forwarded to the target server
-
Host header is modified to match the target server (when
changeOrigin
is enabled) - X-Forwarded-* headers are added for proper proxy identification
- Connection and Content-Length headers are managed automatically
# Preserve all headers (default)
flexible-proxy --route "/api:http://localhost:3000" --preserve-headers
# Don't preserve headers
flexible-proxy --route "/api:http://localhost:3000" --no-preserve-headers
# Don't change the origin header
flexible-proxy --route "/api:http://localhost:3000" --no-change-origin
# Don't change the origin header and preserve all headers
flexible-proxy --route "/api:http://localhost:3000" --no-change-origin --preserve-headers
The proxy supports three logging levels with beautiful formatting:
- Request method, URL, and target with colors
- Error messages with emojis
- Server startup information with ASCII art
- Everything from basic level
- Request headers with structured display
- Response status codes with color coding
- Everything from detailed level
- Request body (for POST/PUT/PATCH) with syntax highlighting
- Response headers and body with full details
Routes can be specified in two ways:
flexible-proxy --route "/api:http://localhost:3000" --route "/auth:https://auth.example.com"
Create a routes.json
file:
{
"/api": "http://localhost:3000",
"/auth": "https://auth.example.com",
"/static": "http://localhost:8080"
}
Then use it:
flexible-proxy --routes-file routes.json
You can also use the package programmatically:
const { createProxyServer } = require("flexible-reverse-proxy");
const routes = {
"/api": "http://localhost:3000",
"/auth": "https://auth.example.com",
};
const server = createProxyServer({
port: 8000,
host: "localhost",
routes: routes,
verbose: true,
logLevel: "detailed",
preserveHeaders: true,
changeOrigin: true,
});
server.start();
flexible-proxy --route "/api:http://localhost:3000"
flexible-proxy \
--route "/api:http://localhost:3000" \
--route "/auth:https://auth.example.com" \
--log-level detailed
flexible-proxy \
--route "/api:http://localhost:3000" \
--log-level full
# Forward all headers without changing origin
flexible-proxy \
--route "/api:http://localhost:3000" \
--no-change-origin \
--preserve-headers
# Minimal header forwarding
flexible-proxy \
--route "/api:http://localhost:3000" \
--no-preserve-headers
# routes.json
{
"/api": "http://localhost:3000",
"/auth": "https://auth.example.com"
}
# Command
flexible-proxy --routes-file routes.json --log-level detailed
# Start with file watching enabled
flexible-proxy --routes-file routes.json --watch
# The server will automatically reload routes when the file changes
# Edit routes.json and save - routes will update without restarting the server
Watch Mode Features:
- Automatically detects changes to the routes file
- Reloads routes without restarting the server
- Validates JSON format before applying changes
- Provides feedback on successful/failed reloads
- Graceful error handling for invalid JSON
- Maintains current routes if file becomes invalid
- Flexible Routing: Configure any URL pattern to any target server
- Pretty Logging: Beautiful colored logs with emojis and structured output
- Comprehensive Logging: Three log levels with detailed request/response information
- Complete Header Forwarding: All headers are forwarded to target servers
- CORS Support: Automatically handles CORS headers for cross-origin requests
- Multiple Configuration Methods: Command line or JSON file
- File Watching: Auto-reload routes when the routes file changes
- Error Handling: Proper error handling for proxy failures
- Preflight Support: Handles OPTIONS requests for CORS preflight
- Request Tracking: Unique request IDs for tracking requests through logs
- Header Control: Options to control header forwarding behavior
# Clone the repository
git clone <repository-url>
cd reverse.proxy
# Install dependencies
npm install
# Run the server with example routes
npm start
# Or run the CLI locally
node bin/cli.js --route "/api:http://localhost:3000" --log-level detailed
npm publish
MIT