2

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")
János
  • 32,867
  • 38
  • 193
  • 353
  • Assuming your server is running [what happens if you add in a closing `/`](https://stackoverflow.com/questions/45367298/could-not-proxy-request-pusher-auth-from-localhost3000-to-http-localhost500): `"proxy": "http://localhost:8080/"`? – Andy Mar 02 '22 at 01:01
  • None of this hint helped: https://stackoverflow.com/questions/45367298/could-not-proxy-request-pusher-auth-from-localhost3000-to-http-localhost500 – János Mar 02 '22 at 01:17

3 Answers3

2

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
    }
  }
1

I solved this issue change localhost to 127.0.0.1 in the package.json over the client project.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
0

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,
    })
  );
};