Create React App http-proxy-middleware not working

Solution 1:

As of v1.0.0 of http-proxy-middleware, the setupProxy.js file requires an explicit import; so instead of the previous default import

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

You need to use:

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

module.exports = function(app) {
    app.use(createProxyMiddleware("/api", { target: "http://localhost:3090" }));
};

Solution 2:

Found the answer to this one - and as expected it was a simple error.

The routes on my express server were as follows:

app.post("/login", requireSignIn, Authentication.login);

Whereas they should have been:

app.post("api/login", requireSignIn, Authentication.login);

Problem solved!

Solution 3:

Replace codes of setupProxy.js file as follows:

const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function(app) {
  app.use(
    '/api/login',
    createProxyMiddleware({
      target: 'http://localhost:3090',
      changeOrigin: true,
    })
  );
};