如何使用 React App 连接多个 API?
使用 http-proxy-middleware 与 Express 后端创建-react-app。
演示错误: https: //github.com/svgpubs/nodeproxy.git
我正在尝试http-proxy-middleware将演示 React 应用程序连接到两个不同的服务器:一个外部网站https://api.coingecko.com/api/和一个内部网站http://localhost:3001/
它适用于外部网站。但是,localhost:3001 连接不起作用。
如果我不使用 http-proxy-middleware (通过添加'proxy: 'http://localhost:3001'"),我可以连接到 localhost:3001 package.json- 但是,那么我只能有一个代理。
这是正在运行的应用程序:如您所见,localhost:3001 没有响应
错误:我尝试了很多不同的变体。我要么从浏览器获取 cors 块,要么 localhost api 从 public/index.html 返回 index.html 文件 - 导致浏览器中出现 json 解析错误。在服务器上,根据 localhost 端点的确切路由,我有时会收到 50 多行此错误:
Error occurred while trying to proxy request /localhostapi/users from localhost:3001 to http://localhost:3001/ (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)
如何设置服务器和代理,以便 App.js 可以连接到 localhost:3001 路由和外部 API?
这是我的App.js
import React, { useEffect, …Run Code Online (Sandbox Code Playgroud) proxy express create-react-app http-proxy-middleware react-fullstack
我使用谷歌应用程序脚本(我们通常使用几乎原始的js,没有库等)。所以我需要使用代理服务器(需要动态改变ip地址)。
但是当我发出请求时,代理服务器会回调
这是一个代理服务器。不响应非代理请求。
通常,在 google app scrip 中发出 http 请求如下所示:
function make_some_request() {
const url = 'https://www.google.com/';
var headers = {
"Content-Type" : "application/json; charset=utf-8",
}
var payload = []; // some data if we need post request
var options = {
"method" : "POST",
'headers' : headers,
'payload' : JSON.stringify(payload)
};
try {
var response = UrlFetchApp.fetch(url, options);
return JSON.parse(response);
} catch (error) {
log("make_some_request", "ERROR: " + error);
}
}
Run Code Online (Sandbox Code Playgroud)
所以,我不明白我应该如何处理使用代理服务器的请求。我们如何以常见方式(在原始 HTTP 中)通过代理服务器进行调用。我应该发送什么标头或有效负载才能使此请求成为代理请求?
PS 据我所知,谷歌应用程序脚本不支持其他类发出 https 请求。所以我所拥有的几乎都是原始的 HTTP。
我正在使用一个 HTTP 请求库,它可以使用http.Agent实例来通过特定代理路由您的请求。让我举一个例子:
const SocksProxyAgent = require('socks-proxy-agent')
const HttpProxyAgent = require('http-proxy-agent') // not used
const axios = require('axios')
// requires Tor service to be running
const torProxyAgent = new SocksProxyAgent('socks://circuit1@127.0.0.1:9050')
// not used
const otherProxyAgent = new HttpProxyAgent('http://admin:1234@localhost:8888')
const axiosConfig = {
httpsAgent: torProxyAgent,
httpAgent: torProxyAgent
}
const axiosInstance = axios.create(axiosConfig)
axiosInstance.get('https://ifconfig.io/ip')
.then(res => {
console.log('Public IP address:', res.data)
}).catch(err => {
console.log(err)
})
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我指定了多个代理 (torProxyAgent和otherProxyAgent),但我只能使用一个。
因此,我正在寻找一种超级代理,我可以将其传递给我的 HTTP 请求库,然后该超级代理通过将所有请求路由到第一个代理,然后通过第二个代理,然后将任意数量的普通代理链接在一起。到第三个,依此类推……
这可能吗?有现成的解决方案吗?
是否有一种方法可以使用Proxy类创建一个假代理,将所有方法调用传递给一个代理,然后传递给第二个代理?
编辑: …
我正在尝试 WSL2 工作,并且我支持公司代理。我已经在 WSL 中配置了代理,因此互联网连接工作正常,并且我已设置 no_proxy 如下
localhost,127.0.0.1,::1
Run Code Online (Sandbox Code Playgroud)
当我使用命令从 wsl 打开 VS Code 时,code .大约需要一分钟才能打开。当我检查 VSCode 启动日志时,它似乎正在尝试连接到 WSL 上此地址的服务器172.17.164.22:36869
我的直觉是,它与代理有关,但即使我将该 IP 地址添加到 no_proxy,它的行为也是一样的。从 WSL1 打开 VSCode 时没有延迟。一分钟后 VSCode 确实加载成功,我可以安装扩展,没有问题,只是初始打开速度很慢。
我尝试从 Windows 和 WSL ping 该地址,但都无法识别它,我按照日志中的建议在浏览器中进行了尝试,它确实立即返回了一个看起来像 guid 的字符串
有任何想法吗?
[2021-03-02 11:39:09.199] Using shell environment from invoking shell: /tmp/vscode-distro-env.D9Yczp
[2021-03-02 11:39:09.392] Probing if server is already installed: C:\WINDOWS\System32\wsl.exe -d Ubuntu-20.04 -e sh -c "[ -d ~/.vscode-server/bin/622cb03f7e070a9670c94bae1a45d78d7181fbd4 ] && printf found || ([ -f /etc/alpine-release ] && printf alpine-; uname …Run Code Online (Sandbox Code Playgroud) 我试图模仿这个已接受的答案 /sf/answers/2782285201/在仅更新某些对象的字段时使用 Hibernate 代理。我的代码如下所示:
@Transactional
public void updateStudentYear(int uniqueNumber, int newYear) {
Student studentProxy = studentRepository.getOne(uniqueNumber);
studentProxy.setYear(newYear);
studentRepository.save(studentProxy);
}
Run Code Online (Sandbox Code Playgroud)
问题在于 Hibernate 既执行完全选择又执行完全更新。这是日志:
2021-03-10 20:24:23.240 DEBUG 118757 --- [ main] org.hibernate.SQL :从学生中选择student0_.unique_number作为unique_n1_1_0_,student0_.name作为name2_1_0_,student0_.team作为team3_1_0_,student0_.year作为year4_1_0_ Student0_ 其中student0_.unique_number=?
2021-03-10 20:24:23.268 DEBUG 118757 --- [ main] org.hibernate.SQL : 更新学生集 name=?, team=?,year=? 其中 unique_number=?
我原以为只会发出一个“更新年份”语句,但令我惊讶的是,看起来一切都是像经典的 findOne()/save() 对操作一样完成的。我错了吗?
我在通过 oauth2-proxy/keycloak 进行身份验证 kubernetes webapp 时遇到问题。你不知道出了什么问题
这三个应用程序单独运行。
认证流程说明:
Keycloak 登录页面显示正确,但用户登录后出现: 500 Internal Server Error with URL https://oauth2-proxy.domain.com/oauth2/callback?state=753caa3a281921a02b97d3efeabe7adf%3Ahttps%3A%2F%2Ftest-app.domain .com%2F&session_state=f5d45a13-5383-4a79-aa7a-56bbaa16056f&code=5344ae72-a9ee-448f-95ef-45e413f69f4b.f5d45a13-5383-4a79-aa7a-56bbaa16056f.7873 2ee5-af17-43fc-9f52-856e06bfce04
来自 oauth2-proxy 的日志
[2021/03/16 11:25:35] [stored_session.go:76] Error loading cookied session: cookie "_oauth2_proxy" not present, removing session
10.30.21.14:35382 - - [2021/03/16 11:25:35] oauth2-proxy.domain.com GET - "/oauth2/auth" HTTP/1.1 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15" 401 13 0.000
10.96.5.198:35502 - - [2021/03/16 11:25:35] oauth2-proxy.domain.com GET - …Run Code Online (Sandbox Code Playgroud) 我正在编写一个执行冗余任务的 python 程序。程序循环一定次数后,我需要能够切换 VPN 服务器并重新开始。所以我基本上尝试使用 Nord VPN 作为 python 中的代理。有人能指出我正确的方向吗?
但是 Bitbucket Server Notifier 失败并出现错误 502:网关错误 - DNS 错误:
Notifying Bitbucket at "https://my.bitbucket.server.url"
Failed to notify Bitbucket for commit g55f45daf0ce678588171ef45g55h34g44 (<html><head><title>502 Bad Gateway</title></head>
<body><h1>DNS error</h1>
<p>DNS error (the host name of the page you are looking for does not exist)<br><br>Please check that the host name has been spelled correctly.<br></p>
<!--Zscaler/6.0--></body></html>
)
Finished: SUCCESS
Run Code Online (Sandbox Code Playgroud)
我感觉这可能是代理问题。有人可以建议吗?
我正在尝试在公司代理后面(在 VPN 上)运行 VS Code .NET 5.0 开发容器。遗憾的是,每次启动容器时,我都无法下载/安装 OmniSharp 扩展。
这是我得到的:
Installing C# dependencies...
Platform: linux, x86_64, name=ubuntu, version=20.04
Downloading package 'OmniSharp for Linux (x64)' Retrying from 'https://roslynomnisharp.blob.core.windows.net/releases/1.37.10/omnisharp-linux-x64-1.37.10.zip' Failed at stage: downloadPackage
Error: connect ECONNREFUSED 127.0.0.1:9000
Run Code Online (Sandbox Code Playgroud)
具体来说,我有一个在本地运行的 zscaler 副本localhost:9000。我可以直接(在 Chrome 中)下载 OmniSharp 包的链接,没有任何问题。我尝试将代理 URL 添加到开发容器 Dockerfile,但没有成功。
ENV HTTPS_PROXY="http://127.0.0.1:9000/localproxy.pac"
ENV HTTP_PROXY="http://127.0.0.1:9000/localproxy.pac"
Run Code Online (Sandbox Code Playgroud)
Docker 声称正在使用 WSL 子系统,并且从 Ubuntu 容器中我可以很好地访问外部世界,所以我不确定下一步要尝试什么。有任何想法吗?
我正在尝试找到任何示例来概述如何采用现有的 React 应用程序并逐渐开始迁移到 Next.js。我已经阅读了所有有关增量采用策略(例如子路径、重写、微前端、代理)的 Next.js 文档,但尚未找到一个可靠的示例来展示这在现实生活中的“我”实际上是什么样子。我们已经有了一个庞大的 React 代码库”场景。
期望的结果是关闭我现有应用程序的子路径(例如 ),/workshop并且/workshop成为 Next.js 应用程序的入口点。然后,我希望能够在两个应用程序之间无缝导航,尽管一旦您进入 Next.js 领域并且需要从代理中导航回来,其机制似乎很模糊。
为了增加上下文,React 和 Next.js 应用程序都将在 Netlify 上提供服务。我已经仔细阅读了他们的文档以及 Vercel 的文档,寻找一个可行的示例,但我仍然不走运。非常感谢任何有关如何最好地解决此类问题的知识或指导!
proxy ×10
http ×2
javascript ×2
bitbucket ×1
express ×1
hibernate ×1
http-proxy ×1
java ×1
jenkins ×1
keycloak ×1
kubernetes ×1
netlify ×1
networking ×1
next.js ×1
nginx ×1
node.js ×1
oauth2-proxy ×1
omnisharp ×1
python ×1
reactjs ×1
socks ×1
spring ×1
vpn ×1
wsl-2 ×1