【发布时间】:2020-11-14 01:27:57
【问题描述】:
我正在通过 FastAPI 提供 React 应用程序 安装
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.route('/session')
async def renderReactApp(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
通过这个 React 应用程序得到服务,并且 React 路由在客户端也可以正常工作
但是一旦客户端重新加载未在服务器上定义但在 React 应用程序 FastAPI 中使用的路由,则返回 not found 来解决此问题,我做了如下操作。
@app.route('/network')@app.route('/gat')@app.route('/session')
async def renderReactApp(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
但这对我来说似乎很奇怪和错误,因为我需要在后端和前端添加每条路线。
我确定 FastAPI 中一定有类似 Flask @flask_app.add_url_rule('/<path:path>', 'index', index) 的东西,它将为所有任意路径提供服务
【问题讨论】:
-
你能分享完整的错误信息吗?
-
嘿@YagizcanDegirmenci 我没有收到任何错误
-
不幸的是,我不是在寻找渲染网络应用程序的方法。我正在寻找一种在 FastAPI 中使用(单路由)可以服务多个路由请求的方法。
@app.route("/some-route") def serveAllRoute(): # servers /some-route as well as /another-woute -
啊,明白了,需要这个解释,请在下面查看我的答案。
标签: python-3.x flask fastapi