1

(First time) I used python -m SimpleHTTPServer 8000 using python 2.7.15 to generate a web server for my simple JavaScript game (still learning), but it seems that it has some error, which makes the canvas of my HTML file wont show.

The JavaScript works totally fine in my GitHub page, but it won't show in local web server. https://jcloh98.github.io/jsgame/simplegame2.html

All the codes are exactly the same as the website. That's all the code I used.

This is the error showed in the console log.

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.

https://jcloh98.github.io/jsgame/simplegame2.html

It is expected to show a black canvas with a aqua paddle at the bottom of the canvas, like https://jcloh98.github.io/jsgame/simplegame2.html

4
  • It means that your python web server is sending "text/plain" as the content type for your javascript files, while it should be "application/javascript". Commented Jul 8, 2019 at 18:43
  • Hi, may I know how can I change from "text/plain" to "application/javascript"? Commented Jul 8, 2019 at 19:05
  • Have a look at the docs. For customising, see here. Commented Jul 8, 2019 at 19:25
  • Hi, thank you very much, the problem is solved. Commented Jul 8, 2019 at 20:05

1 Answer 1

1

Instead of using python -m SimpleHTTPServer 8000, create a .py file named "localhost.py" and run the .py file in the same directory of the webpage.

localhost.py force the file with extension ".js" has "application/javascript" as the content type.

localhost.py

#Use to create local host
import SimpleHTTPServer
import SocketServer

PORT = 8000

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

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "Serving at port", PORT
print(Handler.extensions_map[".js"])
httpd.serve_forever()

Thanks Bergi for helping me to solve this problem out.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.