Skip to content

Instantly share code, notes, and snippets.

@SantoshCode
Created February 8, 2022 05:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SantoshCode/1679ae7150acad1855aa7076ec011487 to your computer and use it in GitHub Desktop.
Save SantoshCode/1679ae7150acad1855aa7076ec011487 to your computer and use it in GitHub Desktop.
Manual Proxy setup in React project

Manual Proxy setup in create-react-app

cd client
npm install http-proxy-middleware
cd src && touch setupProxy.js

setupProxy.js

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

module.exports = function(app) {
  app.use(
    '/api',
    createProxyMiddleware({
      target: 'http://localhost:3001',
      changeOrigin: true,
      pathRewrite: {
          '^/api':'/'
      }
    })
  );
};

So now everyrequest to http://localhost:3000/api goes to http://localhost:3001

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment