Prototyping: Simplest HTTP server with URL routing (to use w/ Backbone.Router)?
-
12-12-2019 - |
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)
La 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()
Autres conseils
Download and install CherryPy
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 useimport 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()))
- Run
python <path\to\always_index.py>
- Point your browser at
http://localhost:8080
and no matter what url you request, you always get the same content.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow