【发布时间】:2021-02-06 03:45:55
【问题描述】:
我尝试使用app.mount 将前端安装到/,但这会使我的所有/api 路由无效。我还尝试使用以下代码将/static 中的文件夹挂载到它们各自的路由并在/ 上提供index.html 文件:
@app.get("/")
def index():
project_path = Path(__file__).parent.resolve()
frontend_root = project_path / "client/build"
return FileResponse(str(frontend_root) + '/index.html', media_type='text/html')
static_root = project_path / "client/build/static"
app.mount("/static", StaticFiles(directory=static_root), name="static")
这主要是可行的,但包含在 client/build 文件夹中的文件未安装,因此无法访问。我知道 Node.js 有一种方法可以通过 res.sendFile("index.html", { root: </path/to/static/folder }); 的相对路径为前端页面提供服务。在 FastAPI 中是否有等效的函数来执行此操作?
【问题讨论】:
-
阅读this 代码,其中
FastAPI和React由单个服务器提供服务。要点是 - 你需要一个运行 React 的进程和一个运行 FastAPI 的进程和一个路由调用的代理服务器(本例中为 nginx)。
标签: reactjs fastapi react-fullstack