# api-forward **Repository Path**: hjnjnj/api-forward ## Basic Information - **Project Name**: api-forward - **Description**: 基于 Node.js 的简易 HTTP 转发代理服务 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-04-03 - **Last Updated**: 2025-04-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: Nodejs ## README # API Forward Proxy 该项目是一个基于 Node.js 的简单 HTTP 转发代理服务,支持跨域请求、超时处理和错误日志记录。 ## 功能特性 - **HTTP 请求转发**:将客户端的请求转发到指定的目标服务器。 - **跨域支持**:自动添加 CORS 相关的响应头。 - **超时处理**:支持自定义请求超时时间,默认 900 秒。 - **错误处理**:记录代理请求失败的详细信息,包括错误码和目标 URL。 ## 安装与运行 1. 确保已安装 [Node.js](https://nodejs.org/)。 2. 克隆或下载本项目到本地。 3. 在项目目录下运行以下命令启动服务: ```bash node index.js ``` 4. 服务启动后,默认监听 `http://127.0.0.1:3000`。 ## 配置说明 ### 目标服务器配置 目标服务器的地址在代码中通过以下方式指定: ```javascript const target = new URL(clientReq.url.startsWith('/') ? clientReq.url : `/${clientReq.url}`, 'http://127.0.0.1:6000'); ``` 可以根据需要修改 `http://127.0.0.1:6000` 为其他目标服务器地址。 ### 超时时间 超时时间通过 `options.timeout` 配置,默认值为 900000 毫秒(15 分钟)。可以根据需要调整: ```javascript timeout: 900000 ``` ### 跨域支持 服务默认支持跨域请求,添加了以下响应头: - `Access-Control-Allow-Origin: *` - `Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS` - `Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With` ### 端口配置 服务默认监听端口为 `3000`,可以在代码中修改: ```javascript const PORT = 3000; ``` ## 日志示例 服务会记录每次请求的详细信息,包括请求方法、URL 和响应状态码。例如: ```plaintext [2025-04-03T08:15:03.571Z] GET /authentication/auth/captchaEnabled 完整代理请求详情: { "method": "GET", "url": "http://127.0.0.1:6000/authentication/auth/captchaEnabled", "headers": { "user-agent": "Apifox/1.0.0 (https://apifox.com)", "accept": "*/*", "host": "116.62.51.161:6000", "accept-encoding": "gzip, deflate, br", "connection": "keep-alive" } } ``` ## 注意事项 - 确保目标服务器地址正确且可访问。 - 如果需要支持 HTTPS,可以使用 `https` 模块进行扩展。