【问题标题】:How to setup proxy in Create React APP for builtin PHP Server?如何在 Create React APP 中为内置 PHP Server 设置代理?
【发布时间】:2021-12-31 15:47:22
【问题描述】:

我只想在 Create React App 中运行内置服务器的 php。

我尝试了多种方法,但没有任何效果。

我尝试将 "proxy" 添加到 package.json 并手动添加代理:

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function(app) {
  app.use(
    '/api/',
    createProxyMiddleware({
      target: 'http://localhost:4000/',
      changeOrigin: true,
    })
  );
};

我运行我的服务器:

php -S localhost:4000 -t ./api

Api 目录有 index.php 文件。而且我从来没有在控制台中看到过请求。当我打开向 /api 发送请求的应用程序时,我看到的是:

Error occured while trying to proxy: localhost:3000/api

和 504(网关超时)。

当我打开 http://localhost:4000/ 时,一切正常。 (是 JSON-RPC 服务显示错误,因为没有 POST 数据)。

【问题讨论】:

    标签: php create-react-app http-proxy


    【解决方案1】:

    GitHub issue找到解决方案:

    代理需要使用IP地址:

    php -S 0.0.0.0:4000 -t ./api
    

    使用 127.0.0.1:4000 作为地址时也可以使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-14
      • 2019-02-07
      • 2020-06-03
      • 2018-09-19
      • 2019-06-10
      • 2021-02-24
      • 2020-10-08
      • 2019-10-29
      相关资源
      最近更新 更多