小编Suh*_*ore的帖子

如何在 FastAPI 中的一条路线上捕获任意路径?

我正在通过挂载从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})
Run Code Online (Sandbox Code Playgroud)

通过这个 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})
Run Code Online (Sandbox Code Playgroud)

但这对我来说似乎很奇怪和错误,因为我需要在后端和前端添加每条路线。

我确定@flask_app.add_url_rule('/<path:path>', 'index', index)FastAPI 中必须有类似 Flask的东西,它将为所有任意路径提供服务

flask python-3.x fastapi

12
推荐指数
3
解决办法
6577
查看次数

标签 统计

fastapi ×1

flask ×1

python-3.x ×1