0

I found similar question here When specified, "proxy" in package.json must be a string ,

but the solution is not working for me , please help me to set up proxy in create-react-app(2.0)

I use "proxy": "http://localhost:8888" in my package.json , but some times it gives error like Error connect from http://localhost:3000 to http://localhost:8888

How to avoid this kind of error ?

Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71

2 Answers2

0

This means your proxy server is not running. Please make sure it's started before or alongside your application!

If your node server is being restarted by Nodemon or something similar, it's expected you see these failed requests every once and a while.

Joe Haddad
  • 1,688
  • 12
  • 11
0

In the client side directory:

npm i http-proxy-middleware --save

Create setupProxy.js file in client/src. No need to import this anywhere. create-react-app will look for this directory

Add your proxies to this file.

const proxy=require("http-proxy-middleware")
module.exports=function(app){
    app.use(proxy(['/api','/auth/google'],{target:"http://localhost:8888"}))}

We are saying that make a proxy and if anyone tries to visit the route /auth/google or /api (specify your routes)on our react server, automatically forward the request on to localhost:8888.

Yilmaz
  • 35,338
  • 10
  • 157
  • 202