Firstly, I just wanted to say that this is my first web application project. I've spent the past few days trying to find answers on how to essentially put the frontend and backend together. I have a lot of questions, but the main one I want answered is on how to return my frontend 'final product' from a backend endpoint.
This is what I understand (please correct me if I'm wrong):
- The frontend code is run by the client (browser).
- When the client interacts with the webpage, the frontend makes API calls to the backend to retrieve/modify data, as necessary.
- The backend and frontend is often developed separately, and could be hosted on separate servers.
- It is, however, possible (and maybe simpler) to host it on a single domain/server. I am hoping to do this, in order to avoid a whole set of issues with CORS.
Then comes the following problem:
When I want to test out my front end and see how it's coming along, I just run npm run start
. I then go to the given url (usually http://localhost:8080/
) and I have access to the frontend that I've developed. And when I want to deploy it, I run npm run build
, which gives me a dist
folder (bundled together and minified).
If I want to run and test my backend locally, as I am using FastAPI
, I simply run uvicorn main:app --reload
.
How to put the two together? More specifically, in my backend code, how do I return the product of my frontend work (i.e., the dist
folder?). I've tried the following (simplified):
@app.get("/", response_class=HTMLResponse)
def root():
return open("../frontend/dist/index.html", "r").read()
but, of course, this only gives me the static html without the React components.
I realize this post may be loaded with incorrect assumptions and poor practices (in which case, my apologies! and I would appreciate any corrections/suggestions.) However, if the following questions could be answered, I would greatly appreciate it. These are questions I have that will hopefully help me test my whole web application locally on my computer.
- How do I return the product of my frontend work for the
GET
request at the domain root endpoint? - If there is a page A, page B, and page C for my web app, each with url
www.example.com/A
,www.example.com/B
, andwww.example.com/C
do I have to create three separate React frontend projects? I.e., equivalent of having threedist
folders? What is the standard way this is handled?