backbone.jsprototypinghttpserversimplehttpserverbackbone-routing

Prototyping: Simplest HTTP server with URL routing (to use w/ Backbone.Router)?


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)


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()