create-react-app proxy仅适用于fetch api,但不适用于浏览器的简单获取

我正在使用在端口3000设置上运行的非常小的create-react-app来代理对在端口8080上运行的后端服务器的请求.
如果我放入浏览器地址栏http:// localhost:3000 / api / me,我会返回index.html页面,但如果我使用fetch API来获取/ api / me,它会尝试通过后端调用.

问题是我必须使用后端进行身份验证,后端会设置一个cookie,但由于我无法访问http:// localhost:3000 / login上的登录页面,我无法获取cookie.

在我从create-react-app中弹出的另一个项目中,我有一个小文件来运行webpack-dev-server和配置

proxy: {
    "*": "http://localhost:9191"
  }

即使放入浏览器地址栏,它也会发出代理请求.

是否可以在create-react-app中创建此类设置?

仔细看看 create-react-app code显示这是设计的:

For single page apps,we generally want to fallback to /index.html.
However we also want to respect proxy for API calls.
So if proxy is specified,we need to decide which fallback to use.
We use a heuristic: if request accepts text/html,we pick /index.html.
Modern browsers include text/html into accept header when navigating.
However API calls like fetch() won’t generally accept text/html.
If this heuristic doesn’t work well for you,don’t use proxy.

在REST控制台扩展中运行http:// localhost:3000 / api / me的GET会返回正确的结果.

关于Fetch API and cookies的进一步阅读揭示我必须包括参数凭证:true以发送cookie:

fetch('/api/me',{
  credentials: 'include'
})

相关文章

react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...
react 本身提供了克隆组件的方法,但是平时开发中可能很少使...
mobx 是一个简单可扩展的状态管理库,中文官网链接。小编在接...
我们在平常的开发中不可避免的会有很多列表渲染逻辑,在 pc ...