crossway
v1.0.0
Published
A simple CORS proxy server to bypass CORS restrictions
Readme
Crossway
A simple CORS proxy server to bypass browser CORS restrictions.
Introduction
Crossway is a lightweight proxy server that helps developers solve cross-origin resource sharing (CORS) restriction issues. When you try to access APIs from different domains in your frontend application, the browser's security policy will block these requests. By using this proxy server, you can easily bypass these restrictions.
Features
- Bypass browser CORS restrictions
- Support for all HTTP methods (GET, POST, PUT, DELETE, etc.)
- Support for HTTPS target sites
- Simple and easy-to-use API
- Provides client-side fetch wrapper
Usage
Before using Crossway to solve cross-domain issues, you need to start the proxy server first. After starting, you can use this relay server to request resources from other domains in any frontend project.
You can start the server in two ways: either by downloading the project locally and starting it, or by installing the crossway package and starting it through programmatic calls.
Direct Server Startup
# Clone the project
git clone https://github.com/kirakiray/crossway
cd crossway
# Install dependencies
npm install
# Use default port 3000 and path "/"
npm start
# Or specify port and path
node index.js --port=30110 --path=/cors-proxyProgrammatically Create and Start Server
npm install crosswayYou can also create and configure the CORS proxy server programmatically:
import { createCorsProxyServer } from "crossway/src/server.js";
// Create proxy server instance
const server = createCorsProxyServer({
port: 30110, // Server port, default is 3000
path: "/cors-proxy" // Server path prefix, default is "/"
});
// Start the server
server.listen(30110, () => {
console.log("CORS Proxy server running on port 30110");
});Sending Requests Through Proxy
1. Direct URL Usage
After starting the server, you can access the target URL in the following way:
http://localhost:30110/cors-proxy?url=https://api.example.com/data2. Using the Provided Fetch Wrapper
You can choose to install the crossway package; or copy the fetch file from the crossway project's src directory to your project;
npm install crosswayThe project provides a fetch wrapper that makes sending requests more convenient:
// import { fetch, config } from "./src/fetch.js";
import { fetch, config } from "crossway/src/fetch.js"; // Need to install crossway first
// Configure proxy server
config.port = 30110;
config.path = "/cors-proxy";
// Send request
const response = await fetch("https://api.example.com/data");
const data = await response.json();Client Example
See the demo.html file for a complete usage example.
API Documentation
Server Endpoints
- GET/POST/PUT/DELETE
/[path]?url=[targetUrl]path: Configured server path prefixurl: Target URL to proxy (needs to be URL encoded)
Response
The proxy server returns the response from the target URL with appropriate CORS headers:
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: Content-Type, AuthorizationConfiguration Options
Command Line Arguments
-p, --port: Server port (default: 3000 or environment variable PORT)-P, --path: Server path prefix (default: "/")
Environment Variables
PORT: Server port (default: 3000)
Development
Project Structure
crossway/
├── index.js # Server entry point
├── demo.html # Usage example
├── package.json # Project configuration
├── LICENSE # License file
├── .gitignore # Git ignore file
├── md/
│ └── cn.md # Chinese documentation
├── src/
│ ├── server.js # Server core logic
│ └── fetch.js # Client fetch wrapper
└── README.md # Project documentationServer Core Logic
The server processes requests through the following steps:
- Verify that the request path matches the configured path prefix
- Add CORS headers to the response
- Handle OPTIONS preflight requests
- Parse the target URL
- Make a request to the target server
- Forward the response to the client
License
This project is licensed under the Apache-2.0 License. See the LICENSE file for details.
