简体   繁体   中英

How to set up proxy in package.json for POST request only?

I have a node app and a create-react-app nested within. Within the development environment, I use concurrently to run two servers, one for the express side, one of the react side.

localhost:5000 for the node/express side localhost:3000 for the React side

At the the moment, I have a sign up page http://localhost:3000/signup and when the user presses the 'Sign Up' button, it makes a POST request to /signup .

I want it so that only the POST request for /signup is proxied to localhost:5000 and not the GET request (which is the view page for the sign up form).

How do I do this?

package.json (in create-react-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"
  }
}

Rest of code: https://github.com/drhectapus/Voting-App

I was looking for an option to do the same but with no luck but I found another solution by including a simple configuration in my webpack.

Being more specific the part where I setup the devServer

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

Hope it helps to somebody who stucks here too.

I had a demo here .

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM