在Nodejs中使用代理的本机提取请求18

发布于 2025-01-31 01:12:31 字数 217 浏览 3 评论 0原文

在节点JS的第18版中,已经有可能在不安装软件包的情况下提取请求(例如:Axios)。

我的问题是,您是否可以在不安装软件包的情况下向本机fetch 提出请求,还是必须安装软件包才能在获取中使用代理?

如果我必须安装一个软件包以在获取中使用代理,那么与Node的新Fetch一起使用的最佳选择是什么?

如果您能留下实施代码,我真的很感激,谢谢!

In version 18 of Node JS there is already the possibility of making request Fetch without installing packages (example: Axios).

My question is if you can make a request to this Native Fetch with Proxy without installing packages or do you have to install packages to use a Proxy in Fetch?

In case I have to install a package to use Proxy in Fetch, what would be the best one to use with Node's new Fetch?

I really appreciate it if you can leave an implementation code, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心碎的声音 2025-02-07 01:12:31

我有代理可以使用本机fetch(),但是我确实必须安装一个软件包(这是因为他们没有公开设置代理机构的功能,请参见在其他答案中提到的这个问题)。对于那些希望为使用本机fetch()的现有软件包设置代理服务器的人(在我的情况下,OctOkit)也可能很有用。

我无法接受它呈现的证书,因为它是用内部根CA签名并返回了错误错误:证书链中的自签名证书self_signed_cert_cert_in_in_chain)。我尝试将Env var node_extra_ca_certs设置为带有所需root ca的文件,但无济于事。我还尝试了在proxyagent ctor和获取本身的选项中指定{velyUnauthorized:false},但没有任何效果。因此,我决定设置env var node_tls_reject_unauthorized。如果有人知道如何使自定义的root cas与本机fetch()和代理使用,请贡献。

安装UNDICI NPM软件包(通过npm install install undiCiYARN添加UNDICI),使用以下内容从https_proxy env var:

import { env } from "process";
import { setGlobalDispatcher, ProxyAgent } from "undici";

if (env.https_proxy) {
  // Corporate proxy uses CA not in undici's certificate store
  process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
  const dispatcher = new ProxyAgent({uri: new URL(env.https_proxy).toString() });
  setGlobalDispatcher(dispatcher);
}

await fetch("https://www.stackoverflow.com");

I got proxies to work with native fetch(), but I did have to install a package (this is because they haven't exposed the functionality to set the proxy nativly, see this issue mentioned in the other answer). This answer might also be useful to those looking to set a proxy server for existing packages that use native fetch() (in my case, Octokit).

I couldn't get it to accept the certificate it presented, as it was signed with an internal Root CA and returned an error Error: self-signed certificate in certificate chain (SELF_SIGNED_CERT_IN_CHAIN). I tried setting the env var NODE_EXTRA_CA_CERTS to a file with the required root CA, to no avail. I also tried specifying {rejectUnauthorized: false} in both the ProxyAgent ctor and the options for the fetch itself, but it didn't have any effect. So I decided to set the env var NODE_TLS_REJECT_UNAUTHORIZED. If anyone knows how to get custom root CAs to work with native fetch() and proxies, please contribute.

After installing the undici NPM package (via npm install undici or yarn add undici), use the following to grab the proxy config from the https_proxy env var:

import { env } from "process";
import { setGlobalDispatcher, ProxyAgent } from "undici";

if (env.https_proxy) {
  // Corporate proxy uses CA not in undici's certificate store
  process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
  const dispatcher = new ProxyAgent({uri: new URL(env.https_proxy).toString() });
  setGlobalDispatcher(dispatcher);
}

await fetch("https://www.stackoverflow.com");
风渺 2025-02-07 01:12:31

节点18不支持本机fetch( https:> https://github.com/nodejs/nodejs/node /eskay/42814 ),但是它正在 https:// github。 com/nodejs/node/essugn/43187 。它似乎没有在节点20的及时实现。

编辑:现在发布节点20,这对我们有用。尽管它确实使用了Node的新获取,但它并不是您所要求的,因为它也需要导入UNDICI支持代理。但是,由于UNDICI也是由节点维护者开发的,并启发了Node的本机获取实现,也许这足够接近。

import { ProxyAgent } from 'undici'
const dispatcher = new ProxyAgent('https://proxy.com')


const r = await fetch('google.com', { 
         dispatcher,
         method: 'POST',
         body: JSON.stringify({ hi: "mom" })
})

Node 18 does not support proxies with native fetch yet (https://github.com/nodejs/node/issues/42814), but it is being worked on https://github.com/nodejs/node/issues/43187. It does not appear to have been implemented in time for Node 20.

Edit: Now with Node 20 released, this is working for us. Though it does use node's new fetch, it isn't quite what you asked for as it also requires an import of undici to support the proxy. But as undici is also developed by Node maintainers and inspired Node's native fetch implementation, maybe this is close enough.

import { ProxyAgent } from 'undici'
const dispatcher = new ProxyAgent('https://proxy.com')


const r = await fetch('google.com', { 
         dispatcher,
         method: 'POST',
         body: JSON.stringify({ hi: "mom" })
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文