1 main.py 파일의 내용을 모두 지우고 다음 코드를 입력한 후 저장(Ctrl + S)합니다.
hello_fastapi/main.py
from fastapi import FastAPI
app = FastAPI()
# 서버 실행
@app.get("/")
def root_handler():
return {"message": "Hello, FastAPI!"}
• from fastapi import FastAPI: fastapi 패키지에서 FastAPI 클래스를 불러옵니다.
• app = FastAPI(): FastAPI 애플리케이션 객체를 생성합니다.
• @app.get("/"): 루트 경로("/")에 GET 요청이 들어왔을 때 실행될 함수를 지정합니다.
• def root_handler(): 루트 경로로 요청이 왔을 때 실행되는 함수입니다.
• return {"message": "Hello, FastAPI!"}: 요청이 들어오면 JSON 형태의 응답을 반환합니다.