Search code examples
webpack-dev-servercreate-react-apphttp-proxy-middleware

CRA - how to proxy all requests but a specific one?


create-react-app docs says you can configure your proxy objects manually. I'm following the http-proxy-middleware docs on matching to exclude a specific route but haven't got it to work.

Basically I'm serving my app from the /app route instead of root. So I want the following to happen:

  1. /app/api proxies to http://localhost:3001, my backend service
  2. All requests that does NOT start with /app proxy to http://cloud.my-app.com

This is what I've tried so far with no luck:

  "homepage": "https://cloud.my-app.com/app",
  "proxy": {
    "/app/api": {                            // Works
      "target": "http://localhost:3001"
    },
    "!/app/*": {                             // Does not work
      "target": "https://cloud.my-app.com",
      "secure": false
    }
  },

What am I missing?


Solution

  • Add the below as your proxy:

      "proxy": {
        "/app/api":{
          "target":"http://localhost:3001",    
       },
        "/.*/":{
          "target":"https://cloud.my-app.com",
          "secure":"false",
          "changeOrigin": true
       }
      }