Performance: Route decorator syntax
This affects the server-side routing setup which indirectly impacts initial response time and client perceived load speed.
from fastapi import FastAPI app = FastAPI() @app.get('/items/{item_id}') async def read_item(item_id: int): # delegate complex logic to async functions result = await fetch_item_data(item_id) return result
from fastapi import FastAPI app = FastAPI() @app.get('/items/{item_id}') def read_item(item_id: int): # complex logic inside route return {'item_id': item_id}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous blocking route handler | 0 | 0 | 0 | [X] Bad |
| Async non-blocking route handler | 0 | 0 | 0 | [OK] Good |
@app.get("/items") decorator do in FastAPI?@app.get() decorator in FastAPI is used to create a route that listens for HTTP GET requests.@app.post() to define routes that handle POST requests.from fastapi import FastAPI
app = FastAPI()
@app.get('/hello')
def say_hello():
return {"message": "Hello, FastAPI!"}@app.get('/hello'), so GET requests to '/hello' will call say_hello().from fastapi import FastAPI
app = FastAPI()
@app.get('/data')
def get_data():
print("Fetching data")
return {"data": 123}print("Fetching data") is not indented properly.@app.get('/data') is correct, and the path starting with '/' is valid in FastAPI.@app.get and @app.post on the same function, which is correct. @app.route('/process', methods=['GET', 'POST']) uses Flask-style syntax which is invalid in FastAPI. Options A and D use non-existent decorators or invalid parameters.Pass the test and this pattern moves unknown