Comet
Comet

Reputation: 73

Localhost refuses to connect when I make setupProxy.js

I made my setupProxy.js to deal with a CORS issue:

const proxy = require('http-proxy-middleware');

module.exports = function (app) {
    app.use(
        proxy('/myUrl', {
            target: 'http://localhost:8090',
            changeOrigin: true
        })
    )
};

After I made it and run 'npm start' again, my browser says that localhost refuses to connect. If I delete setupProxy.js, I can connect to localhost, but the CORS policy blocks to connect with backend server.

Do you have any idea to connect to frontend, still using the setupProxy.js?

Upvotes: 2

Views: 2563

Answers (3)

aarcangeli
aarcangeli

Reputation: 331

I had the same issue.

For me the problem was that I added "type": "module" in my package.json which seems to be unsupported.

Upvotes: 0

samuelmayna
samuelmayna

Reputation: 63

See that you have spelled the name of setupProxy.js well that solved the problem for me. If that does not work. Copy and pase the fb's code the way it is into the setupProxy.js Code can be found here

Upvotes: 0

Panu
Panu

Reputation: 21

You can try this: createProxyMiddleware

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

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

https://stackoverflow.com/a/60354374/11652543

Upvotes: 2

Related Questions