Report this

What is the reason for this report?

Where do I run my FastAPI backend and how to send requests to it from client-side Javascript on a static app?

Posted on June 26, 2024

I have a react app running as a static app. While working on this project in the development phase, I ran my FastAPI backend like this: uvicorn.run(app, host='127.0.0.1', port=5000), and added a "proxy": "http://localhost:5000" to my React app’s package.json file.

I’m unsure of where I’m supposed to run this backend, and how to make requests to it with my Javascript. Currently, I’m trying to send requests with the fetch API like this: fetch("endpoint_name", requestData). Is this correct? If not, what do I change?

Note: The backend Python does have to do some database work with MySQL



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

What you could do here is to use the DigitalOcean App Platform:

https://docs.digitalocean.com/products/app-platform/

First you will start by deploying your React app as a static component as described in this post here:

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-react-application-to-digitalocean-app-platform

After that, you can deploy your FastAPI as a web service component to the same app. That way you will have two separate components both under the same app:

https://docs.digitalocean.com/products/app-platform/how-to/manage-components/

For your backend app, you could set its route to something like /api and then you will be able to call that /api route from your React frontend service.

If your backend service needs a database, you could also add a database service to that app as described here:

https://docs.digitalocean.com/products/app-platform/how-to/manage-databases/

Here is a quick video that shows how you could do that but with a Laravel backend instead:

Hope that this helps!

- Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.