如何在package.json中为POST请求设置代理?

问题描述:

我有一个节点应用程序和一个嵌套在里面的create-react-app。在开发环境中,我同时运行两个服务器,一个用于快速方面,一个是反应方。如何在package.json中为POST请求设置代理?

localhost:5000为节点/快递侧 localhost:3000的阵营侧

在那一刻,我有一个注册http://localhost:3000/signup页面,当用户按下“注册”按钮,它使一个POST请求到/signup

我想它,以便/signup POST请求被代理到localhost:5000,而不是GET请求(这对于注册表单视图页)。

我该怎么做?

的package.json(在创建反应的-APP):

{ 
    "name": "client", 
    "version": "0.1.0", 
    "private": true, 
    "proxy": { 
    "/signup": { 
     "target": "http://localhost:5000" 
    } 
    }, 
    "dependencies": { 
    "axios": "^0.16.2", 
    "lodash": "^4.17.4", 
    "materialize-css": "^0.100.2", 
    "react": "^16.0.0", 
    "react-dom": "^16.0.0", 
    "react-redux": "^5.0.6", 
    "react-router-dom": "^4.2.2", 
    "react-scripts": "1.0.14", 
    "redux": "^3.7.2", 
    "redux-form": "^7.0.4", 
    "redux-thunk": "^2.2.0" 
    }, 
    "scripts": { 
    "start": "react-scripts start", 
    "build": "react-scripts build", 
    "test": "react-scripts test --env=jsdom", 
    "eject": "react-scripts eject" 
    } 
} 

休息的代码:https://github.com/drhectapus/Voting-App

我一直在寻找一个选项,做同样的,但没有运气,但我发现了另一个解决方案是在我的webpack中包含一个简单的配置。

作为更具体的部分,我设置了devServer

devServer: { 
    ... 
    proxy: { 
    '/signup': 'http://localhost:5000' 
    } 
} 

希望它可以帮助别人谁stucks这里。

我有一个演示here

问候。