İnşa Edilmiş - Getirilen ve /#
Node's global fetch is undici. Give it a ProxyAgent İstek başına talep dispatcher seçenek veya global olarak bir kez bir kez bir kez ayarlan setGlobalDispatcher.
import { fetch, ProxyAgent, setGlobalDispatcher } from "undici";
const proxy = new ProxyAgent("http://USER-cc-us:PASS@res.proxy-cyber.com:9000");
// Per request
const res = await fetch("https://api.ipify.org", { dispatcher: proxy });
console.log(await res.text());
// Or globally, for every fetch in the process
setGlobalDispatcher(proxy);A ProxyAgent pools connections, so consecutive requests may reuse one connection and therefore one exit. For a fresh exit per request create the agent with pipelining: 0 ve connections: 1, ya da sadece farklı bir şekilde kullanın -sid- mantıksal oturum için.
axios#
axios'un kendi kendine ait proxy seçenek tünel HTTPS güvenilir değildir; standart yaklaşım bir ajandır https-proxy-agent ve proxy: false.
import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";
const agent = new HttpsProxyAgent("http://USER-cc-de-city-berlin-sid-cart42-ttl-15m:PASS@res.proxy-cyber.com:9000");
const client = axios.create({ httpsAgent: agent, httpAgent: agent, proxy: false, timeout: 30000 });
const { data } = await client.get("https://api.ipify.org");
console.log(data);got#
import got from "got";
import { HttpsProxyAgent } from "https-proxy-agent";
const agent = new HttpsProxyAgent("http://USER-cc-us:PASS@res.proxy-cyber.com:9000");
const body = await got("https://api.ipify.org", { agent: { https: agent, http: agent }, timeout: { request: 30000 } }).text();
console.log(body);Hoffman5#
import fetch from "node-fetch";
import { SocksProxyAgent } from "socks-proxy-agent";
const agent = new SocksProxyAgent(process.env.PROXY_URL);
const response = await fetch("https://api.ipify.org", { agent });
console.log(await response.text());Çevre değişkenleri#
non-ici okumaz HTTPS_PROXY Varsayılan olarak, son / son versiyonlar gemi EnvHttpProxyAgentAncak bunu yapan; küresel sevker ve tüm süreç olarak, getirdiği üçüncü taraf kütüphaneler dahil olmak üzere, proxy üzerinden geçer.
import { EnvHttpProxyAgent, setGlobalDispatcher } from "undici";
// HTTPS_PROXY=http://USER-cc-us:PASS@res.proxy-cyber.com:9000 NO_PROXY=localhost node app.js
setGlobalDispatcher(new EnvHttpProxyAgent());Eşleştirme ve rotasyon#
- Bir Biri Bir
ProxyAgentper sticky session (farklı seans)-sid-), bu seansın tüm istekleri tarafından paylaşılan. - Geniş bir rotasyon için, bir ajan tut ama bağlantı tekrarını devre dışı bırakmak veya N seans ids ve round-robin ile N ajanları çalıştırın.
- Watch
UND_ERR_SOCKETveECONNRESETBir konut çıkışı gitti; bir kez yeniden deneme, bir sonraki bağlantı başka bir tane alır.
Soru: İnsanlar soruyor#
Neden anxios HTTPS için proxy görmezden geliyor?
İnşa edilmiş proxy desteği, CR tünelleri için sınırlıdır. httpsAgent ve set proxy olarak https://www.agent'i kullanın: false so axios kendini idare etmeye çalışmaz.
Sadece bazı proxy talepleri olabilir miyim?
Evet. Kargocu veya acenteyi buna ihtiyaç duyan ve başkalarına zorlanmış olan taleplere geçin; GlobalDispatcher'ı aramadığınız sürece hiçbir şey küresel değildir.