Search code examples
javascriptnode.jsreactjscreate-react-app

Proxy in production React for fetch API express


I was developping a app with React app. In developing env i was using proxy but I'm deploying the app and I saw that proxy didn't work in.

I read about http-proxy-middleware. It can be a solution or it don't works too?

Any way to do this without config the server with redirects to other port?

I need to continue fetching to my API server.


Solution

  • The best way what I found without configure server and NGINX is follow this steps:

    1. Build front
    2. Move folder into a backend server.
    3. Put that code after routes:

      if (process.env.NODE_ENV === 'production') {
         app.use(express.static(`${__dirname}/yourFrontFolder/build`));
         app.get('*', (req, res) => {
          res.sendFile(`${__dirname}/yourFrontFolder/build/index.html`);
         })
         ...
      

      And build your backend code and access to your backend port like frontend.