Question

We're working on a Backbone.js application and the fact that we can start a HTTP server by typing python -m SimpleHTTPServer is brilliant.

We'd like the ability to route any URL (e.g. localhost:8000/path/to/something) to our index.html so that we can test Backbone.Router with HTML5 pushState.

What is the most painless way to accomplish that? (For the purpose of quick prototyping)

Was it helpful?

Solution

Just use the built in python functionality in BaseHTTPServer

import BaseHTTPServer

class Handler( BaseHTTPServer.BaseHTTPRequestHandler ):
    def do_GET( self ):
        self.send_response(200)
        self.send_header( 'Content-type', 'text/html' )
        self.end_headers()
        self.wfile.write( open('index.html').read() )

httpd = BaseHTTPServer.HTTPServer( ('127.0.0.1', 8000), Handler )
httpd.serve_forever()

OTHER TIPS

  1. Download and install CherryPy

  2. Create the following python script (call it always_index.py or something like that) and also replace 'c:\index.html' with the path of your actual file that you want to use

    import cherrypy
    
    class Root:
        def __init__(self, content):
            self.content = content
    
        def default(self, *args):
            return self.content
        default.exposed = True
    
    cherrypy.quickstart(Root(open('c:\index.html', 'r').read()))
    
  3. Run python <path\to\always_index.py>
  4. Point your browser at http://localhost:8080 and no matter what url you request, you always get the same content.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top