組み込みのフェッチと Undic#
ノードのグローバル fetch ウンディです。それを与えてください ProxyAgent リクエストごとに dispatcher オプションを使用するか、グローバルに一度設定します 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 接続をプールするため、連続するリクエストは 1 つの接続を再利用し、したがって 1 つの終了を再利用する可能性があります。リクエストごとに新たに終了するには、次のようにエージェントを作成します。 pipelining: 0 そして connections: 1、または単純に別のを使用します -sid- 論理セッションごとに。
アクシオス#
アクシオス独自の proxy このオプションは HTTPS を確実にトンネリングしません。標準的なアプローチは、エージェントからの https-proxy-agent そして 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);得た#
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);ソックス5#
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());環境変数#
ウンディは読みません HTTPS_PROXY デフォルトでは。最近の Undic バージョンが出荷されました EnvHttpProxyAgent、それはそうなります。これをグローバル ディスパッチャとして設定すると、フェッチ上に構築されたサードパーティ ライブラリを含むプロセス全体がプロキシを経由します。
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());同時実行とローテーション#
- 1 つ
ProxyAgentスティッキー セッションごと (異なる-sid-)、そのセッションのすべてのリクエストで共有されます。 - 広範囲にローテーションする場合は、1 つのエージェントを保持して接続の再利用を無効にするか、N 個のセッション ID を持つ N 個のエージェントを実行してラウンドロビンします。
- 見る
UND_ERR_SOCKETそしてECONNRESET: 住宅の出口がなくなった。一度再試行すると、次の接続では別の接続が選択されます。
人々が尋ねる質問#
axios が HTTPS のプロキシを無視するのはなぜですか?
組み込みのプロキシ サポートは、CONNECT トンネルに限定されています。 https-proxy-agent を httpsAgent として使用し、proxy: false を設定して、axios がそれ自体を処理しようとしないようにします。
一部のリクエストのみをプロキシできますか?
はい。必要なリクエストについてはディスパッチャまたはエージェントに渡し、その他のリクエストはそのままにしておきます。 setGlobalDispatcher を呼び出さない限り、グローバルなものはありません。