mediapipe icon indicating copy to clipboard operation
mediapipe copied to clipboard

Can't Serve Files Locally in Python Due to CORS

Open aderantcurt opened this issue 1 year ago • 0 comments

If you're trying to serve these files locally using python, you may encounter this error in the Chrome console because of CORS attempting to serve TEXT file MIME types instead of JS file types.

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.

If you want to serve these using python and run it locally in Chrome, this may work for you:

Create a file named custom_server.py with these contents:

# From
# https://stackoverflow.com/a/60638762/2387067
#Use to create local host
import http.server
import socketserver

PORT = 8000
SERVER_ADDRESS = "127.0.0.1"

Handler = http.server.SimpleHTTPRequestHandler
Handler.extensions_map.update({
      ".js": "application/javascript",
});

httpd = socketserver.TCPServer((SERVER_ADDRESS, PORT), Handler)
httpd.serve_forever()

and then run it

python -m custom_server.py

Then browse to:

http://127.0.0.1:8000/

aderantcurt avatar May 17 '24 19:05 aderantcurt