FastAPI 静态文件 MIME 类型返回错误导致 ESM 报错 Strict MIME type checking?-灵析社区

代码大师

同样一个Web应用。家里使用正常。复制到公司电脑启动后访问就报错。 Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec. Chrome/Firefox都这样。 用fastapi写的。代码文件都是复制过去的。 app.mount("/static", StaticFiles(directory="static"), name="static") app.mount("/main/assets", StaticFiles(directory="static/assets"), name="static") @app.get("/") async def redirect_to_static(): return RedirectResponse(url="/main") @app.get("/main", response_class=HTMLResponse) async def static_endpoint(request: Request): return templates.TemplateResponse("static/index.html", {"request": request}) import uvicorn if __name__ == '__main__': file = Path(__file__) uvicorn.run(app=f'{file.stem}:app', host="127.0.0.1", port=8000, reload=True) Vite App ![Snipaste_2024-04-09_11-49-52.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241003/7f1c7152ad2a61dcc0085570e8a11668.png) ![Snipaste_2024-04-09_12-22-53.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241003/e1d9479a18f469de0e867c989ffad93b.png)

阅读量:119

点赞量:0

问AI
类似问题: "https://github.com/pallets/flask/issues/1045" (https://link.segmentfault.com/?enc=rrKPe6jZAEid%2B6kvvM43uQ%3D%3D.eAeZ79f99guyURahh6aJjZpoIR9gvU1kBO%2BoQM%2FlZAWpiQUFxvro8kbdOTC3to2q) 虽然👆这个是 Flask 不是 FastAPI,但原因都一样,就是 Windows 注册表因为未知原因损坏导致 MIME 类型映射有问题。 具体解决方案见评论区。