By Hemanta Sundaray on 2021-05-21
Install http-proxy-middleware package using npm.
npm i http-proxy-middleware
Create a setupProxy.js file in the src directory.
Add your proxies to the setupProxy.js file as shown below.
const { createProxyMiddleware } = require("http-proxy-middleware");
module.exports = function (app) {
app.use(
["/api/users/login", "/api/users/register"],
createProxyMiddleware({
target: "http://localhost:5000",
})
);
};
NOTE: You don't need to import this file anywhere. It is automatically registered when you start the development server. Also, anytime you make a change to the setupProxy.js file, you’ll need to restart your server.
For more information, check out the following resourse:
https://create-react-app.dev/docs/proxying-api-requests-in-development/