【问题标题】:How to make a static site made with Backbone respond to dynamic urls to the site如何使使用 Backbone 制作的静态站点响应站点的动态 url
【发布时间】:2016-03-01 23:43:02
【问题描述】:

我正在使用 Backbone 创建一个静态站点,该站点只有一个文件作为入口点(index.html)。我想知道如何让网站响应网站的任何 external url?例如

www.example.com/this_route www.example.com/search

虽然我现在确实设置了路由器,但我只能在应用内触发 url 更改:

router.navigate( "to_here" );

但是,如果我在浏览器的 url 栏中键入 www.example.com/to_here,我会收到错误消息:“在此服务器上找不到请求的 URL /to_here。”

感谢您的帮助

【问题讨论】:

  • “虽然我现在确实设置了路由器” 请提及您的设置细节,以便我们了解问题所在/提供建议。您目前使用什么 http 服务器来服务 index.html..?

标签: javascript backbone.js backbone-routing


【解决方案1】:

您需要将您的网络服务器设置为始终以index.html 响应。

我为此目的使用nginx,并遵循以下规则,它始终为像localhost:8080/to_here这样的任何请求提供index.html

server {
    listen       8080;
    server_name  localhost;

    location / {
        root   /path/to/folder/with/files;
        try_files $uri /index.html;
    }
}

【讨论】:

  • 哦,有趣,谢谢。我想我可以用 apache 做同样的事情吗?
  • 我没有使用过 Apache,抱歉。另外不要忘记允许服务器处理对 .js、.css 和其他资源的请求。
  • @bleepbloop 我已经用 apache tomcat 设置了你想要的功能。您需要设置一个过滤器来捕获所有请求并相应地重新路由它们。让所有资产/ajax 请求通过,并将其他所有内容重新路由回 index.html
【解决方案2】:

在 Backbone 中,当它们以“#”开头时,您会从浏览器中“捕获”url。所以,即使你的路由器也是这样的:

routes : {
          "this_route":"exampleFunction",
          "search":"searchFunction"
      }

如果你想根据浏览器url执行每个函数,你必须写www.example.com#this_routewww.example.com#search到使用 Router.js 触发函数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-28
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 2015-01-27
    相关资源
    最近更新 更多