Once you are ready to move to production, you'll need to have the backend and frontend running on the same domain (to take advantage of things like HTTP-Only cookies, for example).
You can build the static React app files like so:
cd frontend
npm run build
But now how do you actually serve these from your FastAPI backend?
In my application I tried doing something like this:
api = FastAPI(root_path="/api")
# Create all my backend routes using @api.get() and @api/post() ...
app = FastAPI()
app.mount("/api", api)
app.mount("/", StaticFiles(directory="frontend/build", html=True), name="static")
This sort of works. I am able to get to my root frontend index by going to http://localhost:5000
, but when I try to navigate to any other route in my React app (as defined by react-router-dom
) I just get a 404 Not Found from the backend.
Any ideas?