Jordi Castillo
Jordi Castillo

Reputation: 165

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.

Upvotes: 1

Views: 2031

Answers (2)

Jordi Castillo
Jordi Castillo

Reputation: 165

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.

Upvotes: 1

Greg
Greg

Reputation: 65

You don't usually need a proxy in your React app when it is deployed. To deploy, you usually run npm run build, which creates a build directory containing all the compiled JavaScript and HTML files you need for the deployment. These are then served by a web server, such as NGINX or by your backend application.

Upvotes: 0

Related Questions