3

I want to use SimpleHTTPServer to serve my local site while I'm developing. I'm using basic javascript, HTML, and CSS. I have this kind of project structure:

  • app (folder with src files)
  • dist (build folder where everything is located for a host)
    • assets (css, js, etc)
    • services (html files for different services)
      • name_of_service_1.html
      • name_of_service_2.html
    • index.html
    • services.html
  • package.json
  • gulp.js

Inside navigation I have a basic structure for every link, something like this:

<a href="/services">Services</a>
<a href="/services/name_of_service_1">Service 1</a>

Besides this, I'm using HTML preload, so that pages are loaded faster if someone hovers over those links. Because of that, I can't use services.html or etc, because in that case, preload won't work. I'm using netlify to host this site, and there everything works fine.

My question: How to serve locally with SimpleHTTPServer but that page will load nicely without .html extension in the link.

1 Answer 1

1

Here is how to do it:

import http.server
from http.server import HTTPServer, BaseHTTPRequestHandler
import socketserver

PORT = 8080

Handler = http.server.SimpleHTTPRequestHandler

Handler.extensions_map={
    '.html': 'text/html',
    '': 'text/html', # Default is 'application/octet-stream'
    }

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

print("serving at port", PORT)
httpd.serve_forever()

Reference

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.