RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1600341
Accepted
S.H.
S.H.
Asked:2024-11-20 14:23:53 +0000 UTC2024-11-20 14:23:53 +0000 UTC 2024-11-20 14:23:53 +0000 UTC

FastAPI:如何将应用程序托管和静态文件结合起来?

  • 772

这里我使用 FastAPI 编写“Hello world”:

main.py 文件说

import uvicorn
from fastapi import FastAPI

app = FastAPI()

if __name__ == '__main__':
    uvicorn.run("main:app", host='0.0.0.0', port=80, reload=True)

@app.get("/api")
async def root():
    return {"message": "Hello World"}

在此之前我安装了依赖项

pip install fastapi
pip install uvicorn

一切都很好,我启动了应用程序python main.py,当我通过 url 输入浏览器时,http://127.0.0.1/api我收到了珍贵的 json{"message":"Hello World"}

然后我添加静态文件托管:我创建一个静态目录,将 index.html 放在那里并添加一些行,然后更改我的 python 文件,如下所示:

import uvicorn
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles

app = FastAPI()
app.mount('/', StaticFiles(directory='static',html=True))

if __name__ == '__main__':
    uvicorn.run("main:app", host='0.0.0.0', port=80, reload=True)

@app.get("/api")
async def root():
    return {"message": "Hello World"}

同时,托管静态文件对我有用 - http://127.0.0.1/index.html 通过 url 打开,但是......对同一 url 的请求http://127.0.0.1/api停止工作

此外,如果我将设置静态文件路由的行更改为

app.mount('/static/', StaticFiles(directory='static',html=True))

那么两个请求,byhttp://127.0.0.1/static/和 by http://127.0.0.1/api- 都会正常工作。

似乎存在路由冲突:沿较短路径的路由/“中断”沿该路径的路由/api

我如何确保 index.html 已加载,并且位于“短”url 处http://127.0.0.1/,并且我的 api 也能正常http://127.0.0.1/api工作?

让我解释一下:我想基于FastAPI制作一个“穷人网站”:这样就可以从中加载带有JS的页面,加载后开始访问api

谢谢你!

python
  • 1 1 个回答
  • 33 Views

1 个回答

  • Voted
  1. Best Answer
    S.H.
    2024-11-20T15:45:29Z2024-11-20T15:45:29Z

    非常感谢 insolor 和 CrazyElf!

    您指出了要寻找的内容,并在这里找到了现成的答案: https://stackoverflow.com/questions/60716529/download-file-using-fastapi

    所以这是我想要的第一个工作版本:

    import uvicorn
    from fastapi import FastAPI
    from starlette.responses import FileResponse
    
    app = FastAPI()
    
    @app.get("/")
    async def read_index():
        return FileResponse('index.html')
    
    @app.get("/api")
    async def root():
        return {"message": "Hello World"}
    
    if __name__ == '__main__':
        uvicorn.run("main:app", host='0.0.0.0', port=80, reload=True)
    

    这是第二个:(就我个人而言,我更喜欢通过 mount 来安装路径,而不是通过“魔法属性”)

    import uvicorn
    from fastapi import FastAPI
    from fastapi.staticfiles import StaticFiles
    
    app = FastAPI()
    
    @app.get('/api')
    def api_app():
        return {"message": "Hello World"}
    
    app.mount('/api', app)
    app.mount('/', StaticFiles(directory='static',html=True))
    
    if __name__ == '__main__':
        uvicorn.run("main:app", host='0.0.0.0', port=80, reload=True)
    
    • 1

相关问题

  • 是否可以以某种方式自定义 QTabWidget?

  • telebot.anihelper.ApiException 错误

  • Python。检查一个数字是否是 3 的幂。输出 无

  • 解析多个响应

  • 交换两个数组的元素,以便它们的新内容也反转

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5