I want to serve my React frontend using FastAPI. The goal being 0 Javascript dependency for the user. The user can simply download the Python code, start server, and view the website on localhost.
My folder structure is:
- my-fullstack-app
- frontend/
- build/
- public/
- ...
- package.json
- backend/
- main.py
- static/
I ran npm run build
to generate the frontend/build/
folder which contains:
build/
├── asset-manifest.json
├── favicon.ico
├── index.html
├── logo192.png
├── logo512.png
├── manifest.json
├── robots.txt
└── static
├── css
│ ├── main.073c9b0a.css
│ └── main.073c9b0a.css.map
├── js
│ ├── 787.cda612ba.chunk.js
│ ├── 787.cda612ba.chunk.js.map
│ ├── main.af955102.js
│ ├── main.af955102.js.LICENSE.txt
│ └── main.af955102.js.map
└── media
└── logo.6ce24c58023cc2f8fd88fe9d219db6c6.svg
I copied the contents of the frontend/build/
folder inside backend/static/
.
Now, I want to serve this backend/static/
folder via FastAPI as opposed to running another server.
In my FastAPI's main.py
I have:
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
app = FastAPI()
app.mount("/", StaticFiles(directory="static/"), name="static")
I then start the server using - uvicorn main:app --reload
.
But it doesn't work.
When I open http://127.0.0.1:8000/
in the browser, the output is a JSON file which says {"detail":"Not Found"}
and console has Content Security Policy: The page's settings blocked the loading of a resource at http://127.0.0.1:8000/favicon.ico ("default-src").
.
How do I get this to work? I've seen examples for similar functionality with React and Express.