Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 原型设计:使用URL路由的最简单HTTP服务器(使用w/Backbone.Router)?_Backbone.js_Prototyping_Httpserver_Simplehttpserver_Backbone Routing - Fatal编程技术网

Backbone.js 原型设计:使用URL路由的最简单HTTP服务器(使用w/Backbone.Router)?

Backbone.js 原型设计:使用URL路由的最简单HTTP服务器(使用w/Backbone.Router)?,backbone.js,prototyping,httpserver,simplehttpserver,backbone-routing,Backbone.js,Prototyping,Httpserver,Simplehttpserver,Backbone Routing,我们正在开发一个Backbone.js应用程序,我们可以通过键入python-msimplehttpserver来启动HTTP服务器,这一点非常棒 我们希望能够将任何URL(例如,localhost:8000/path/to/something)路由到我们的index.html,这样我们就可以用HTML5pushState测试主干.Router 实现这一目标最简单的方法是什么?(用于快速原型制作) 下载并安装 创建以下python脚本(称之为always_index.py或类似的东西),并将“c

我们正在开发一个Backbone.js应用程序,我们可以通过键入
python-msimplehttpserver
来启动HTTP服务器,这一点非常棒

我们希望能够将任何URL(例如,
localhost:8000/path/to/something
)路由到我们的
index.html
,这样我们就可以用HTML5
pushState
测试
主干.Router

实现这一目标最简单的方法是什么?(用于快速原型制作)

  • 下载并安装

  • 创建以下python脚本(称之为
    always_index.py
    或类似的东西),并将“c:\index.html”替换为要使用的实际文件的路径

    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()))
    
  • 运行
    python
  • 将浏览器指向
    http://localhost:8080
    无论您请求什么url,您都会得到相同的内容

  • 只需使用
    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()
    

    localhost/path\u to\u nowhere
    将引导您到
    nowhere
    。但是,
    localhost/path/where\u in\u nowhere
    可以像您期望的那样引导您到达任何地方。要将任何url路由到端点(目标url),必须首先加载包含主干、下划线和jquery的页面,对吗?另外,您应该引导一个路由到端点url的路由器。我想您误解了我的问题。当您使用支持pushState的主干应用程序时,您的URL将更改为
    http://localhost:8000/some/path/in/my/app
    浏览应用程序时。但是使用SimpleHTTPServer,如果此时刷新页面,您将得到404,因为服务器尝试获取该URL的资源。我正在寻找一种方法,允许服务器将任何URL重新路由回
    index.html
    。如果您有一些文件确实要按原样加载,该怎么办?@snapfractalpop我不知道您在问什么。这将按原样发送
    index.html
    。我想知道您希望按原样加载的静态资产(而不是为它们提供index.html)。我为我拙劣的评论道歉。@snapfractalpop啊,我明白了。您必须编写一些代码来解析
    self.path
    ,并相应地路由请求。尽管对于真正简单的路由之外的任何事情,我可能会建议使用flask(或其他替代方法),而不是自己用
    BaseHTTPServer
    重新实现HTTP路由。