最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I use ES6 modules on python http.server? - Stack Overflow

programmeradmin8浏览0评论

I'm instantiating a module like so in index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Hello Module</title>
  </head>
  <body>
    <script type="module" src="./index.js"></script>
  </body>
</html>

index.js is empty.

When I serve this via py -3 -m http.server (Python 3.8.5), I get the Chrome error:

Failed to load module script: The server responded with a non-JavaScript MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec.

I've read what this error signifies. I do not know how to configure http.server because heretorfore it's been a black box to me. Am I past the point at which Python's default (?) server is helpful?

I'm instantiating a module like so in index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Hello Module</title>
  </head>
  <body>
    <script type="module" src="./index.js"></script>
  </body>
</html>

index.js is empty.

When I serve this via py -3 -m http.server (Python 3.8.5), I get the Chrome error:

Failed to load module script: The server responded with a non-JavaScript MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec.

I've read what this error signifies. I do not know how to configure http.server because heretorfore it's been a black box to me. Am I past the point at which Python's default (?) server is helpful?

Share Improve this question edited Jul 30, 2020 at 6:14 The Most Curious Thing asked Jul 30, 2020 at 5:32 The Most Curious ThingThe Most Curious Thing 2113 silver badges14 bronze badges 6
  • Please show us the server code too. – AKX Commented Jul 30, 2020 at 5:36
  • See issue35403 on the Python issue tracker - you may need to use Python 3.8 as per the resolution. – metatoaster Commented Jul 30, 2020 at 5:42
  • @AKX That's my point of confusion; I don't know where or what http.server's code is. – The Most Curious Thing Commented Jul 30, 2020 at 5:45
  • No, please show the code where you use http.server. – AKX Commented Jul 30, 2020 at 5:46
  • @AKX Okay, my question is more thorough now. – The Most Curious Thing Commented Jul 30, 2020 at 6:15
 |  Show 1 more ment

1 Answer 1

Reset to default 11

JavaScript should be served with the content-type text/javascript.

The default http.server.SimpleHTTPRequestHandler handler may not (since mappings can be read from the Windows registry) have a mapping for the .js extension to a file type, or it might be wrong (as evident from text/plain).

You'll need to write your own short script to patch in the .js extension, and use that instead of python -m http.server, something like:

import http.server

HandlerClass = http.server.SimpleHTTPRequestHandler

# Patch in the correct extensions
HandlerClass.extensions_map['.js'] = 'text/javascript'
HandlerClass.extensions_map['.mjs'] = 'text/javascript'

# Run the server (like `python -m http.server` does)
http.server.test(HandlerClass, port=8000)
发布评论

评论列表(0)

  1. 暂无评论