为什么我在Windows 10上无法读取Deno代理环境变量?

3

我正在使用 Windows 10,阅读 deno 手册发现需要设置环境变量 HTTPS_PROXY 才能让 fetch() 使用代理。

我已经执行了以下操作:

Deno.env.set('HTTP_PROXY', 'https://138.68.60.8:3128/');
Deno.env.set('HTTPS_PROXY', 'https://138.68.60.8:3128/');

然而,即使没有代理,fetch仍然可以正常获取内容。相反,如果我在Windows系统设置中设置代理,则fetch将使用该代理。但这并没有解决问题,因为我正在尝试使用一个代理列表。


我用不同的方法回答了这个问题。如果出于某种原因需要在环境变量中完成此操作,我猜测Deno会在运行时一次性读取环境变量(而不是每个获取请求都读取)。这意味着您必须在脚本运行之前设置环境变量。可以通过以下方式完成:https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-7.2 - IndevSmiles
1个回答

0

查看 Deno 源代码时,他们自己的测试使用了一种我在文档中找不到的不同方法:

// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

const client = Deno.createHttpClient({
  proxy: {
    url: "http://localhost:4555",
    basicAuth: { username: "username", password: "password" },
  },
});

const res = await fetch(
  "http://localhost:4545/045_mod.ts",
  { client },
);
console.log(`Response http: ${await res.text()}`);

client.close();

针对您的特定用例,我会设置一个HTTP客户端:

const client = Deno.createHttpClient({
  proxy: {
    url: "https://138.68.60.8:3128",
  },
});

然后在您的fetch请求中使用该客户端:

fetch("https://example.com", { client });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接