Error React Proxy error: Could not proxy request

Viewed 5864

Why do I got this error:

Proxy error: Could not proxy request /api/v1/management/me from localhost:3000 to http://localhost:8080 (ECONNREFUSED).

Got this axios setting:

axios.defaults.baseURL = "http://localhost:3000/";

And set this in package.json:

"proxy": "http://localhost:8080"

tried also:

"proxy": "http://localhost:8080/"

And have following call:

axios({
  method: "get",
  url: "api/v1/management/me",
  data: {},
  headers: { crossDomain: true },
})

When I call directly http://localhost:8080/api/v1/management/me I got response from server.

I have following backend, Vapor route setting. Maybe something wrong / specific here?

let protectedAPIRouter = authSessionRouter.grouped("api/v1").grouped(User.guardAuthMiddleware())
let managementAPIRouter = protectedAPIRouter.grouped("management")
3 Answers

I would suggest the following solutions:

  1. Try to change localhost to IP address: "proxy": "http://your_IP_address:8080"
  2. Try this construction also:
"proxy": {
    "/api/*":  {
      "target": "http://localhost:8080",
      "secure": false
    }
  }

Got this error with the 2nd solution you propose:

Connected to Mongodb
[1] When specified, "proxy" in package.json must be a string.
[1] Instead, the type of "proxy" was "object".
[1] Either remove "proxy" from package.json, or make it a string.

You need to set up a proxy to the backend service you want to access.

  1. First install (http-proxy-middleware) package.
  2. Remove the ("proxy": "http://localhost:8080/") or the Url behind the proxy in the package json file.
  3. In the src folder create a file (setupProxy.js) and and the following code.

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function(app) {
  app.use(
    '/api',
    createProxyMiddleware({
      target: 'http://localhost:8080',
      changeOrigin: true,
    })
  );
};

Related