'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?



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Panu