2

看来我的简单 Starlette/Uvicorn Web 应用程序为 Jinja 模板化(从同一服务器提供)JavaScript 文件提供了不正确的 MIME 内容类型。从屏幕截图中可以看出,uvicorn 服务器将 *.js 文件转换为类型(“text/plain”)。

截屏

我已经搜索了 Starlette 和 Uvicorn 的文件,简直被难住了。

我的简单网络应用程序如下:

from starlette.applications import Starlette
from starlette.staticfiles import StaticFiles
from starlette.responses import HTMLResponse
from starlette.templating import Jinja2Templates
from starlette.middleware.cors import CORSMiddleware
import uvicorn
from random import randint

port = randint(49152,65535)

templates = Jinja2Templates(directory='templates')


app = Starlette(debug=True)
app.mount('/static', StaticFiles(directory='statics', html=True), name='static')
app.add_middleware(
    CORSMiddleware, allow_origins=["*"], allow_headers=["*"], allow_methods=["*"]
)

@app.route('/')
async def homepage(request):
    template = "index.html"
    context = {"request": request}
    return templates.TemplateResponse(template, context, media_type='text/html')


@app.route('/error')
async def error(request):
    """
    An example error. Switch the `debug` setting to see either tracebacks or 500 pages.
    """
    raise RuntimeError("Oh no")


@app.exception_handler(404)
async def not_found(request, exc):
    """
    Return an HTTP 404 page.
    """
    template = "404.html"
    context = {"request": request}
    return templates.TemplateResponse(template, context, status_code=404)


@app.exception_handler(500)
async def server_error(request, exc):
    """
    Return an HTTP 500 page.
    """
    template = "500.html"
    context = {"request": request}
    return templates.TemplateResponse(template, context, status_code=500)


if __name__ == "__main__":
    uvicorn.run("app-567:app", host='0.0.0.0', port=port, log_level="info", http='h11', loop='asyncio', reload=True)

加载到头部的 JavaScript 文件会给出相同的错误,但仍然会加载。这是 Firefox(73.0 64 位)中新的“nosniff”默认设置的副产品。作为模块导入加载的脚本完全失败。

我正在运行 Windows 10 (x64)、Python 3.7、uvicorn 0.11.2 和 starlette 0.13.1。

任何帮助是极大的赞赏。先感谢您。

4

1 回答 1

3

我可以通过显式设置mimetypes变量来解决此问题,如下所示:

from starlette.applications import Starlette
from starlette.staticfiles import StaticFiles
from starlette.responses import HTMLResponse
from starlette.templating import Jinja2Templates
from starlette.middleware.cors import CORSMiddleware
import uvicorn
from random import randint

import mimetypes
mimetypes.init()

port = randint(49152,65535)

templates = Jinja2Templates(directory='templates')


app = Starlette(debug=True)
app.mount('/static', StaticFiles(directory='statics', html=True), name='static')
app.add_middleware(
    CORSMiddleware, allow_origins=["*"], allow_headers=["*"], allow_methods=["*"]
)

@app.route('/')
async def homepage(request):
    mimetypes.add_type('application/javascript', '.js')
    mimetypes.add_type('text/css', '.css')
    mimetypes.add_type('image/svg+xml', '.svg')
    template = "index.html"
    context = {"request": request}
    return templates.TemplateResponse(template, context, media_type='text/html')


@app.route('/error')
async def error(request):
    """
    An example error. Switch the `debug` setting to see either tracebacks or 500 pages.
    """
    raise RuntimeError("Oh no")


@app.exception_handler(404)
async def not_found(request, exc):
    """
    Return an HTTP 404 page.
    """
    template = "404.html"
    context = {"request": request}
    return templates.TemplateResponse(template, context, status_code=404)


@app.exception_handler(500)
async def server_error(request, exc):
    """
    Return an HTTP 500 page.
    """
    template = "500.html"
    context = {"request": request}
    return templates.TemplateResponse(template, context, status_code=500)


if __name__ == "__main__":
    uvicorn.run("app-567:app", host='0.0.0.0', port=port, log_level="info", http='h11', loop='asyncio', reload=True)
于 2020-02-17T21:09:07.410 回答