Node.js 高级隧道代理:undici / superagent / https 客户端

Node.js undici、superagent、https 原生模块三种高级 HTTP 客户端对接亿牛云爬虫代理。

亿牛云技术团队2026年4月23日1 分钟阅读

undici

undici 是 Node.js 官方的新一代 HTTP 客户端,通过 ProxyAgent 支持代理:

import { request, ProxyAgent } from 'undici';

const proxyUrl = `http://${user}:${pass}@${host}:${port}`;

// 场景 A:强制切换 IP
for (let i = 1; i <= 3; i++) {
    const proxyAgent = new ProxyAgent(proxyUrl);
    const resp = await request('https://httpbin.org/ip', {
        dispatcher: proxyAgent,
        headers: { 'Connection': 'close' },
    });
    const data = await resp.body.json();
    console.log(`请求 ${i}: ${data.origin}`);
    proxyAgent.close();
}

// 场景 B:保持 IP
const proxyAgent = new ProxyAgent(proxyUrl);
for (let i = 1; i <= 3; i++) {
    const resp = await request('https://httpbin.org/ip', {
        dispatcher: proxyAgent,
        headers: { 'Connection': 'keep-alive' },
    });
    const data = await resp.body.json();
    console.log(`请求 ${i}: ${data.origin}`);
}
proxyAgent.close();

undici 的 ProxyAgent 支持 headers 选项在 CONNECT 阶段注入自定义头(HTTPS Tunnel 需验证):

const proxyAgent = new ProxyAgent({
    uri: proxyUrl,
    headers: { 'Proxy-Tunnel': 'undici-demo' },
});

superagent

superagent 通过 .agent() 方法挂载 https-proxy-agent

import superagent from 'superagent';
import { HttpsProxyAgent } from 'https-proxy-agent';

const proxyUrl = `http://${user}:${pass}@${host}:${port}`;

// HTTPS Tunnel 通过 proxyOpts.headers
const agent = new HttpsProxyAgent(proxyUrl, {
    headers: { 'Proxy-Tunnel': 'superagent-demo' },
});

const resp = await superagent
    .get('https://httpbin.org/ip')
    .agent(agent)
    .timeout({ response: 15000 });

console.log(resp.text);

https 原生模块(HTTPS CONNECT 头注入)

Node.js 原生 https 模块配合 https-proxy-agent 可以精细控制 CONNECT 阶段:

import { HttpsProxyAgent } from 'https-proxy-agent';

const agent = new HttpsProxyAgent(proxyUrl, {
    headers: { 'Proxy-Tunnel': 'https-demo' },
});

const req = https.request({
    hostname: 'httpbin.org',
    path: '/ip',
    agent,
    method: 'GET',
}, (res) => {
    let data = '';
    res.on('data', chunk => data += chunk);
    res.on('end', () => console.log(data));
});
req.end();

三种方案对比

特性undicisuperagenthttps + agent
性能最高
API 风格标准回调/async链式 .then()事件驱动
HTTPS Tunnel⚠️ 需验证
HTTP/2
适用场景高性能后端通用请求底层控制

需要企业代理方案?

我们可根据目标站点、并发规模与稳定性目标提供定制方案。