在 Bun 中,fetch
支援透過 HTTP 或 HTTPS 代理伺服器發送請求。這在企業網路或當您需要確保請求透過特定 IP 位址發送時非常有用。
await fetch("https://example.com", {
// The URL of the proxy server
proxy: "https://username:password@proxy.example.com:8080",
});
proxy
選項是一個 URL 字串,用於指定代理伺服器。如果代理伺服器需要驗證,它可以包含使用者名稱和密碼。它可以是 http://
或 https://
。
您也可以設定 $HTTP_PROXY
或 $HTTPS_PROXY
環境變數為代理 URL。當您想要對所有請求使用相同的代理伺服器時,這非常有用。
HTTPS_PROXY=https://username:password@proxy.example.com:8080 bun run index.ts