A secure, production-ready CORS proxy server built with Node.js and Express. This proxy enables frontend applications to bypass CORS restrictions when accessing third-party APIs, with built-in authentication, rate limiting, and comprehensive test coverage.
- π‘οΈ Secure Authentication - API key-based authentication to prevent unauthorized access
- β‘ Rate Limiting - Configurable request limits to prevent abuse
- π CORS Enabled - Full CORS support with preflight request handling
- π Dynamic Routing - Automatically routes requests to any target URL
- π Comprehensive Testing - Full test suite with 100% coverage
- βοΈ Flexible Configuration - Environment-based or programmatic configuration
- π Production Ready - Error handling, logging, and middleware orchestration
- Node.js (v14 or higher)
- npm or yarn
git clone <repository-url>
cd cors-proxy-1
npm install- Copy the example environment file:
cp .env.example .env- Edit
.envand configure your settings:
API_KEYS=your-secret-key-1,your-secret-key-2
MAX_REQUESTS_PER_MINUTE=60
PORT=8088# Production
npm start
# Development
npm run devThe server will start on http://localhost:8088 (or your configured port).
To proxy a request through the server, prepend the target URL to the proxy server path:
// Example: Proxying a request to https://api.example.com/data
fetch('http://localhost:8088/https://api.example.com/data', {
headers: {
'x-api-key': 'your-secret-key-1'
}
})
.then(response => response.json())
.then(data => console.log(data));curl -H "x-api-key: your-secret-key-1" \
http://localhost:8088/https://api.example.com/dataThe proxy extracts the target URL from the request path:
- Proxy URL:
http://localhost:8088/https://api.example.com/endpoint - Target URL:
https://api.example.com/endpoint
| Variable | Description | Default |
|---|---|---|
API_KEYS |
Comma-separated list of valid API keys | - |
MAX_REQUESTS_PER_MINUTE |
Maximum requests per minute per client | 60 |
PORT |
Server port | 8088 |
const { createApp } = require('./proxy');
const app = createApp({
apiKeys: ['key1', 'key2'],
maxRequests: 100,
windowMs: 60000 // 1 minute in milliseconds
});
app.listen(3000);The project includes a comprehensive test suite covering:
- β Authentication
- β Rate limiting
- β CORS functionality
- β Middleware order
- β Configuration
- β Error handling
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Generate coverage report
npm run test:coverage- CORS - Enables cross-origin requests
- JSON Parser - Parses JSON request bodies
- Authentication - Validates API keys
- Rate Limiter - Enforces request limits
- Proxy - Forwards requests to target URLs
cors-proxy-1/
βββ proxy.js # Main server and proxy logic
βββ proxy.test.js # Comprehensive test suite
βββ package.json # Project dependencies and scripts
βββ .env.example # Example environment configuration
βββ .env # Your environment configuration (not in git)
βββ readme.md # This file
- API Key Authentication: All requests require a valid API key in the
x-api-keyheader - Rate Limiting: Prevents abuse with configurable request limits
- Error Handling: Graceful error handling prevents information leakage
- CORS Protection: Properly configured CORS headers
| Code | Description |
|---|---|
200 |
Successful proxy request |
401 |
Missing or invalid API key |
429 |
Rate limit exceeded |
500 |
Proxy or server error |
The proxy is designed to be extensible. The createApp function returns an Express app that can be enhanced with additional middleware or routes.
const { createApp } = require('./proxy');
const app = createApp();
app.use((req, res, next) => {
console.log(`${req.method} ${req.path}`);
next();
});- Frontend Development: Access APIs without CORS restrictions during development
- Third-Party API Integration: Bypass CORS when integrating external APIs
- API Aggregation: Combine multiple API calls through a single proxy
- Rate Limit Management: Control and monitor API usage across applications
Contributions are welcome! Please ensure all tests pass before submitting a pull request.
npm testISC
- Verify your API key is set correctly in
.env - Ensure the
x-api-keyheader is included in your requests
- Adjust
MAX_REQUESTS_PER_MINUTEin.env - Consider implementing per-key rate limiting for different user tiers
- Check that the target URL is valid and accessible
- Verify the target server is running and responding
- Review server logs for detailed error messages
For issues and questions, please open an issue on the GitHub repository.
Built with β€οΈ using Node.js, Express, and modern JavaScript