简体   繁体   中英

create-react-app proxy api requests behind a corp proxy

Here is my situation,

I am creating a react app behind a corp firewall. My api server is running remotely on a different location. How to setup my proxy in the package.json so that I can hit the api servers through the corp proxy?

My configuration so far I have experimented with is this:

"proxy" : { 
     "/api/" : {
          "target" : "http://****myapiserver.com",
          "changeOrigin" : true,
          "agent" : {
              "host" : "pathtoproxy.com",
              "port" : 80
           }
      }
 }

I have tried several other combinations also without any luck. Anything I'm missing or wrong?

Also, my npm is configured, so that it has http and https proxy setup.

Okay, so I solved this by writing a custom proxy - so now I have 3 proxies.

  1. React Proxy - Which will be hit when the browser makes a call for the api.
  2. My custom proxy that is "pipe-ing" the request from react app's proxy to corp proxy.
  3. CORP Proxy, that allows me to reach the api server I want to hit.

     // React App's package.json - runs on port 3000 "proxy" : "http://localhost:3001" // myproxy.js - runs on port 3001 var express = require('express') var request = require('request') const app = express(); var serverRequest = request.defaults({'proxy':'http://path2.corp.proxy.com:port'}) app.all('/', (req, res) => { let url = "http://myserver.com/api" + req.url req.pipe(serverRequest(url)).pipe(resp) ); app.listen(3001); 

Once I have this, I start my custom proxy as node proxy.js Then I start my react app and everything works well, I can hit the api server.

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