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