FastApi APIRouter引用

  • 每一个路由都增加一个通用的路径前缀

  • 每一个路由都有对应的tag

- routers/user.py  --> usersRouter=APIRouter()
- routers/items.py --> itemsRouter=APIRouter()

# main.py
from fastapi import FastAPI
from routers.user import usersRouter
from routers.items import itemsRouter

app = FastAPI()
app.include_router(usersRouter)
app.include_router(itemsRouter)



app.include_router(usersRouter,prefix="/user",tags=['users'])
app.include_router(itemsRouter,prefix="/items",tags=['Itmes'])
  • 如果在所有路由前面添加统一的前缀路径,那么重复使用 APIrouter

Last updated