[FIXED] Can a React app communicate with Flask and express backend both at once?


I have a React app with a Flask backend listening on port 5000 on localhost (flask’s default). Can I add another backend using express to the same web app that also listens on port 5000? but I wanted to have multiple backends working from the same frontend.

To clarify, my use case is like this:

  • When a user sends a POST request to her Flask, the data is saved to a local database.
  • Use the values ​​saved from the local database (the response from Flask) for further processing on the express server.
  • Redirect the user to the appropriate page in React using the response from the fast server.
  • Most online tutorials only have one backend, but I wanted to work with a more complex system. I was thinking of the following ideas –

  • Call fetch('flask/api/save' from React and @app.route('flask/api/save') to Flask backend Save data to code> Save data to local database
  • Once you get a successful response from the Flask backend, call another fetch('express/api/doWork). Here we use the same port 5000 to connect to the updated local database. Send Flask from Express using app.get('/express/api/doWork) and send the appropriate response.
  • Redirect the user to the correct page based on the response.
  • Is this possible? Or is there a better way to achieve this communication?

    Thank you!


    I finally figured out the answer myself. It’s not possible for 2 servers to listen on the same port. The solution here would be to use Flask as a reverse proxy and redirect the client calls to the express server.

    Reference – Multiple backend servers accessible from a Flask server

    Answered By – addybist

    Answer Checked By – Gilberto Lyons (Easybugfix Admin)

    Leave a Reply

    (*) Required, Your email will not be published