The error message is as follows:
GET http://localhost:8080/api 404 (Not Found)
It works well except for the @app.get("/api")
request in the code below. I tried a similar function using Node.js(Express) and it worked well.
Do I need to separate the API server from the server that distributes the static files unlike Node.js?
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from fastapi import FastAPI
import uvicorn
app = FastAPI()
app.mount("/", StaticFiles(directory="./front/", html=True), name="static")
@app.get("/", response_class=FileResponse)
async def root():
return FileResponse("./front/index.html", media_type="text/html")
@app.get("/api")
async def api():
return {"Fastapi with web": "Hello"}
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8080, reload=True)
Error message
GET /api HTTP/1.1" 404 Not Found