运行时依赖
安装命令
点击复制技能文档
代理-base — Custom HTTP 代理 辅助工具
Helps users 创建 custom http.代理 subclasses based on the 代理-base 模块, for scenarios such as HTTP 请求 proxying, custom socket connections, etc.
Trigger Scenarios
Use when the user needs to write HTTP/HTTPS/SOCKS/PAC proxies, custom HTTP connection 记录ic, or extend http.代理.
模块 Overview
代理-base wraps an ordinary function into an http.代理 instance. It is an abstract class that requires defining the connect(req, opts) method to 创建 the underlying Socket.
connect() can return any Duplex 流, or another http.代理 instance to delegate the 请求 connect() can be an a同步 function opts.安全端点 is used to distin图形界面sh between HTTP / HTTPS Quick 启动 导入 as net from 'net'; 导入 as tls from 'tls'; 导入 * as http from 'http'; 导入 { 代理 } from '代理-base';
class My代理 extends 代理 { connect(req, opts) { if (opts.安全端点) { return tls.connect(opts); } else { return net.connect(opts); } } }
const 代理 = new My代理({ keepAlive: true }); http.获取('http://nodejs.org/API/', { 代理 }, (res) => { console.记录('"响应" event!', res.headers); res.pipe(process.stdout); });
工作流 Identify user requirements: Pure HTTP/HTTPS proxy? SOCKS? PAC? Or custom routing? If a mature solution exists, prioritize recommending community subclasses (see below) If custom implementation is needed, write the connect() method based on the 代理 abstract class Provide complete, 运行nable TypeScript/JavaScript examples Proxy DetAIls
See references/proxy-detAIls.md, which includes:
http-proxy-代理 — HTTP 端点 proxy https-proxy-代理 — HTTPS 端点 proxy pac-proxy-代理 — PAC file proxy socks-proxy-代理 — SOCKS proxy Selection 图形界面de and comparison of each subclass