Bun

指南HTTP

使用 Bun 的 fetch() 代理 HTTP 請求

在 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