4

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)

Dan
  • 9,912
  • 18
  • 49
  • 70
  • `localhost/path_to_nowhere` will lead you to `nowhere`. But `localhost/path/somewhere_in_nowhere` can be made to lead you to anywhere as you expected. To Route any url's to endpoint(the target url) you must first load the page that contains backbone,underscore,jquery right? also you should have bootstrapped a router that routes to endpoint url. – Deeptechtons May 21 '12 at 10:26
  • I think you misunderstood my question. When you're in a pushState-enabled Backbone app, your URL will change to `http://localhost:8000/some/path/in/my/app` as you navigate through your app. But with SimpleHTTPServer, if you refresh the page at this point, you will get a 404 since the server tries to fetch the resource for that URL. I'm looking for a way to allow the server to re-route any URL back to `index.html`. – Dan May 22 '12 at 07:28

2 Answers2

3

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()
obmarg
  • 9,369
  • 36
  • 59
  • 1
    What if you have some files that you do want to load as is? – snapfractalpop Nov 23 '14 at 02:32
  • @snapfractalpop I'm not sure what you're asking. This will send `index.html` as is. – obmarg Nov 24 '14 at 19:48
  • I was wondering about static assets that you want to load as is (and not serve up index.html for them). I apologize for my poorly worded comment. – snapfractalpop Nov 24 '14 at 21:50
  • @snapfractalpop Ah, I see. You'd have to write some code to parse `self.path` and route the requests accordingly. Though for anything other than really simple routing I'd probably recommend using flask (or an alternative) rather than re-implementing HTTP routing yourself with `BaseHTTPServer`. – obmarg Nov 25 '14 at 00:50
1
  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.
Steve
  • 2,205
  • 1
  • 21
  • 28