Currently migrating a personal project that was written in Express.js over to FastAPI. So far, after googling and scanning through FastAPI documentation, I haven't seen the equivalent of a next() or router.use() that I've utilized in Express.js (see more here). Is there something perhaps in the documentation that I missed? Or a possible work around for this particular issue? Any help/guidance is greatly appreciated!
It seems you are looking for https://fastapi.tiangolo.com/tutorial/bigger-applications/
I am currently using APIRouter() in my code now. I didn't really see my answer on that part of the website, thus my question (I am new to FastAPI as a whole). Would it be the equivalent of just adding another app.include() with a file going to another router with no path operations?
This is my main.py file
#!/usr/bin/python
import uvicorn
from fastapi import FastAPI
from app.router import router
app = FastAPI()
app.include_router(router)
if __name__=='__main__':
uvicorn.run(app, host='localhost')
my router.py file is below
#!/usr/bin/python
from fastapi import APIRouter
from app.controllers.first_controller import FirstController
from app.controllers.second_controller import SecondController
from starlette.requests import Request
from starlette.responses import JSONResponse
router = APIRouter()
@router.post("/sendData")
async def fc_func(request: Request):
dataToSend = await FirstController().check_for_info(request)
return JSONResponse({"info":"{}".format(dataToSend)})
@router.post("/tryit"):
async def sc_func(request: Request):
responseText = await SecondController().tryIt_func(request)
return JSONResponse(responseText)
In my use case, am I adding just another async def func() to my code in router.py with no decorator or do I need to create another app.include_router(new_router_file.py) which goes to different routes that have no decorators? Hope this gives a better idea of what I'm trying to do.
So I figured it out. In your router, just call the async def func() call from any of your other functions and it should work. You can pass objects that way as well
Most helpful comment
It seems you are looking for https://fastapi.tiangolo.com/tutorial/bigger-applications/