在Nodejs中使用代理的本机提取请求18
在节点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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有代理可以使用本机
fetch()
,但是我确实必须安装一个软件包(这是因为他们没有公开设置代理机构的功能,请参见在其他答案中提到的这个问题)。对于那些希望为使用本机fetch()
的现有软件包设置代理服务器的人(在我的情况下,OctOkit)也可能很有用。我无法接受它呈现的证书,因为它是用内部根CA签名并返回了错误
错误:证书链中的自签名证书
(self_signed_cert_cert_in_in_chain
)。我尝试将Env varnode_extra_ca_certs
设置为带有所需root ca的文件,但无济于事。我还尝试了在proxyagent
ctor和获取本身的选项中指定{velyUnauthorized:false}
,但没有任何效果。因此,我决定设置env varnode_tls_reject_unauthorized
。如果有人知道如何使自定义的root cas与本机fetch()
和代理使用,请贡献。安装
UNDICI
NPM软件包(通过npm install install undiCi
或YARN添加UNDICI
),使用以下内容从https_proxy env var:
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 nativefetch()
(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 varNODE_EXTRA_CA_CERTS
to a file with the required root CA, to no avail. I also tried specifying{rejectUnauthorized: false}
in both theProxyAgent
ctor and the options for the fetch itself, but it didn't have any effect. So I decided to set the env varNODE_TLS_REJECT_UNAUTHORIZED
. If anyone knows how to get custom root CAs to work with nativefetch()
and proxies, please contribute.After installing the
undici
NPM package (vianpm install undici
oryarn add undici
), use the following to grab the proxy config from thehttps_proxy
env var:节点18不支持本机fetch( https:> https://github.com/nodejs/nodejs/node /eskay/42814 ),但是它正在 https:// github。 com/nodejs/node/essugn/43187 。它似乎没有在节点20的及时实现。
编辑:现在发布节点20,这对我们有用。尽管它确实使用了Node的新获取,但它并不是您所要求的,因为它也需要导入
UNDICI
支持代理。但是,由于UNDICI
也是由节点维护者开发的,并启发了Node的本机获取实现,也许这足够接近。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 asundici
is also developed by Node maintainers and inspired Node's native fetch implementation, maybe this is close enough.