是否可以将 html get 请求从 create-react-app 代理到 /graphql?
Is it possible to proxy the html get request to /graphql from a create-react-app?
我有一个 create-react-app 应用程序,我在其中通过添加启用了代理:
"proxy": "http://localhost:3001",
给我的 package.json。这适用于对 /graphql 的 API 请求,但是当 Web 浏览器请求 /graphql(为了加载 UI 到 运行 查询的目的)由前端处理而不是代理时.是否也可以代理它?
当我尝试通过访问 https://localhost:3000/auth/facebook 来执行 OAuth 时,同样的事情发生了,前端处理它而不是后端。
确实可以进一步自定义代理。
首先,当网络浏览器请求 /graphql
或 /auth/facebook
时,它会发送包含 text/html
(以及其他)的 Accept
header。 CRA 代理 specifically ignores 的配置请求具有此 header 值:
The development server will only attempt to send requests without text/html in its Accept header to the proxy.
(原文着重)
幸运的是,您可以覆盖默认配置并根据您的喜好基本上挂接代理中间件。 There are detailed instructions in the docs and even more so in the npm package docs,但症结在于:
app.use(
'/graphql',
createProxyMiddleware({
target: 'http://localhost:3001',
changeOrigin: true,
})
);
我有一个 create-react-app 应用程序,我在其中通过添加启用了代理:
"proxy": "http://localhost:3001",
给我的 package.json。这适用于对 /graphql 的 API 请求,但是当 Web 浏览器请求 /graphql(为了加载 UI 到 运行 查询的目的)由前端处理而不是代理时.是否也可以代理它?
当我尝试通过访问 https://localhost:3000/auth/facebook 来执行 OAuth 时,同样的事情发生了,前端处理它而不是后端。
确实可以进一步自定义代理。
首先,当网络浏览器请求 /graphql
或 /auth/facebook
时,它会发送包含 text/html
(以及其他)的 Accept
header。 CRA 代理 specifically ignores 的配置请求具有此 header 值:
The development server will only attempt to send requests without text/html in its Accept header to the proxy.
(原文着重)
幸运的是,您可以覆盖默认配置并根据您的喜好基本上挂接代理中间件。 There are detailed instructions in the docs and even more so in the npm package docs,但症结在于:
app.use(
'/graphql',
createProxyMiddleware({
target: 'http://localhost:3001',
changeOrigin: true,
})
);