Is there a FastAPI equivalent to next() and router.use() that is in Express.js? #7472
-
|
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 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
It seems you are looking for https://fastapi.tiangolo.com/tutorial/bigger-applications/ |
Beta Was this translation helpful? Give feedback.
-
|
I am currently using 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 |
Beta Was this translation helpful? Give feedback.
-
|
So I figured it out. In your router, just call the |
Beta Was this translation helpful? Give feedback.
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