Node.js Advanced Tunnel Proxy: undici / superagent / https
Node.js undici, superagent, and native https module integrating 16Yun Crawler Proxy.
16Yun Engineering TeamApr 23, 20261 min read
undici
undici is Node.js's official next-generation HTTP client, supporting proxies via ProxyAgent:
import { request, ProxyAgent } from 'undici';
const proxyUrl = `http://${user}:${pass}@${host}:${port}`;
// Scenario A: Force IP switch
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(`Request ${i}: ${data.origin}`);
proxyAgent.close();
}
// Scenario B: Keep 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(`Request ${i}: ${data.origin}`);
}
proxyAgent.close();undici's ProxyAgent supports the headers option to inject custom headers during CONNECT (HTTPS Tunnel needs verification):
const proxyAgent = new ProxyAgent({
uri: proxyUrl,
headers: { 'Proxy-Tunnel': 'undici-demo' },
});superagent
superagent mounts https-proxy-agent via the .agent() method:
import superagent from 'superagent';
import { HttpsProxyAgent } from 'https-proxy-agent';
const proxyUrl = `http://${user}:${pass}@${host}:${port}`;
// HTTPS Tunnel via 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 Native Module (HTTPS CONNECT Header Injection)
Node.js's native https module with https-proxy-agent provides fine-grained CONNECT control:
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();Comparison of Three Approaches
| Feature | undici | superagent | https + agent |
|---|---|---|---|
| Performance | Highest | Medium | Medium |
| API Style | Standard callback/async | Chained .then() | Event-driven |
| HTTPS Tunnel | Caveat: Needs verification | Yes | Yes |
| HTTP/2 | Yes | No | No |
| Use Case | High-performance backend | General requests | Low-level control |
Need an enterprise proxy plan?
We can tailor architecture to your target domains, concurrency, and reliability goals.