【问题标题】:Proxy in package.json not affecting fetch requestpackage.json 中的代理不影响获取请求
【发布时间】:2017-11-06 00:40:51
【问题描述】:

我正在尝试使用 React 从开发服务器获取一些数据。

我在localhost:3001 上运行客户端,在port 3000 上运行后端。

获取请求:

 const users = fetch('/api/users');
    users.then((err,res) => {
      console.log(res);
    })

当我运行我的开发服务器和 webpack-dev-server 时,我得到以下输出:

GET http://localhost:3001/api/users 404 (Not Found)

我尝试在 package.json 中指定代理,以便它将请求代理到 API 服务器,但是没有任何改变。

这是我的 package.json 文件

.. 和 webpack.config

如果您需要查看我的项目中的其他内容,请告诉我。很抱歉,如果我遗漏了什么并且不够彻底,我对使用这些技术还是很陌生。

【问题讨论】:

  • 如果你的服务器在 localhost:3000 上运行,那么修改你的 fetchRequest 像 fetch('http://localhost:3000/api/users') 也确保你的后端启用了 cors
  • 能解决你的问题吗
  • 是的,我想将请求代理到 api 服务器,但是它不起作用,但是直接向 http://localhost:3000/api/users 发出请求是正常的。
  • 我认为问题出在您的命名约定上 :)
  • 这些 cmets 没有回答如何有效使用代理的问题。

标签: node.js reactjs api proxy create-react-app


【解决方案1】:

您可以修改您的获取请求 API url 以提供完整的主机名,因为

 fetch('http://localhost:3000/api/users') 

还要确保您在后端启用了CORS

如果你想通过 webpack 重定向,你可以试试devServer.proxy as

devServer: { 
    inline: true, 
    contentBase: './dist', 
    port: 3001, 
    proxy: { "/api/**": { target: 'http://localhost:3000', secure: false }  }
 }

【讨论】:

  • 你永远不应该启用 cors。
  • @AakashVerma,我不完全同意您的评论,当您需要向不同域上的前端提供数据时,您需要启用 cors,但是,我同意您可能想要对您信任的域进行过滤
  • 我想补充的另一件事是,如果您的代理不使用 https,请确保 https: false,否则会导致问题。
【解决方案2】:

我知道我在这里玩游戏有点晚了,但我会把它留在这里以供将来参考。

要使 devServer 代理按预期工作,您需要将 HTTP Accepts 标头指定为“text/html”以外的其他内容。使用 fetch 接受的 init-object 作为第二个参数执行此操作。一个简单的例子:

fetch("/api/profile",{
    headers:{
        "accepts":"application/json"
    }
})
.then(res => {
    console.log(res);
    return res.json();
})
.then(json => console.log(json) )
.catch( a => { console.log(a) });

这样做的原因是 WebPack 开发服务器通常使用上下文/命名空间来区分要服务的内容和转发的内容。 create-react-app 脚本不会从 package.json 文件中的代理路径中提取命名空间。相反,脚本具有自以为是的默认行为,即任何使用 HTTP GET 以外的东西的请求都将被转发。此外,任何使用 HTTP GET 但不是 text/html 作为 Accepts 标头的内容都将被转发。

原因是因为大多数 React 应用程序都是 SPA(单页应用程序),它们使用 AJAX/Fetch 与某些 API 进行通信。 API 通常使用 JSON 或 XML,但不使用 text/html

【讨论】:

  • 感谢 Canis。这是我缺少的一块!
  • 谢谢。这是我缺少的链接。干杯。
【解决方案3】:

在 package.json 中

"proxy": {
    "/api/users": {
        "target": "http://localhost:3000"
    }
},

【讨论】:

  • 遇到错误:When specified, "proxy" in package.json must be a string. Instead, the type of "proxy" was "object". Either remove "proxy" from package.json, or make it a string.
【解决方案4】:

https://github.com/webpack/webpack-dev-server/issues/793#issuecomment-316650146 中用户 jellyfish-tom 的解决方案对我有用。

devServer: {
  proxy: {
    "*": "http://[::1]:8081"
    // "secure": false,
    // "changeOrigin": true
  }
},

【讨论】:

    【解决方案5】:

    Webpack 开发服务器在您的 Webpack 配置中使用 devServer.proxy config 来控制代理请求。

    【讨论】:

    • 感谢您的快速回复。我尝试在 devServer 中指定代理,但它仍然没有将请求重定向到 api 服务器。 devServer: { inline: true, contentBase: './dist', port: 3001, proxy: { "/api": "http://localhost:3000" } } 网络选项卡中的常规标题:Request URL:http://localhost:3001/api/users Request Method:GET Status Code:304 Not Modified Remote Address:127.0.0.1:3001
    猜你喜欢
    • 2014-02-24
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 2022-01-09
    • 2018-12-30
    相关资源
    最近更新 更多