GBProxies

Node.js (fetchとaxios)でプロキシを使用する方法

Node.js 18以降はundici経由でfetchを提供します。ProxyAgentはユーザー名とパスワードでゲートウェイ経由で各リクエストを送信します。

手順

  1. npm install undiciでundiciをインストール
  2. ゲートウェイURLとログインでProxyAgentを作成
  3. fetchのdispatcherとして渡す
  4. 必要に応じてユーザー名に国コードまたはセッションIDを追加

コード例

import { ProxyAgent, fetch } from "undici";

const agent = new ProxyAgent({
  uri: "http://gw.gbproxies.cc:10000",
  token: "Basic " + Buffer.from("USERNAME-cc-de:PASSWORD").toString("base64"),
});
const r = await fetch("https://api.ipify.org?format=json", { dispatcher: agent });
console.log(await r.json());

USERNAMEとPASSWORDをコンソールのProxiesページのプロキシログインに置き換えてください。ポート10000は住宅用HTTPです。各製品のポートは以下を参照してください。

ヒント

  • axiosの場合、proxy: { protocol: "http", host, port, auth: { username, password } }を設定
  • 複数の固定IPを同時に必要とする場合、セッションIDごとに1つのエージェントを作成

よくある質問

HTTPSサイトで動作しますか?

はい。エージェントはターゲットへのCONNECTトンネルを開くため、HTTPSはエンドツーエンドで暗号化されたままです。

Node.js (fetchとaxios)でプロキシを使用する方法