简体   繁体   English

让骨干路由与pushstate和node.js / express一起使用作为服务器

[英]Getting backbone routing to work with pushstate and node.js/express as server

I'm trying to build a single page app with backbone.js on front end and node.js/express as server, I want to have a base HTML file for the root, and then when user navigates to any path such as 我正在尝试使用前端的backbone.js和node.js / express作为服务器构建单页应用程序,我希望为根提供基本HTML文件,然后当用户导航到任何路径时,例如

mydomain.com/foo/bar

I want to be able to handle that path on the client side by javascript instead of making a round trip to server. 我希望能够通过javascript在客户端处理该路径,而不是往返于服务器。 I am reading about backbone routing and HTML5 push state. 我正在阅读骨干路由和HTML5推送状态。 In this article he describes push state like this, 这篇文章中,他描述了像这样的推送状态,

In fact, PushState is really nothing more than a standard API for JavaScript, that allows us to manipulate the browser history by “push”ing full URLs into the browser's URL without making a round trip to the server, and respond to changes in the URL with Javascript – all without the use of URL hash fragments. 实际上,PushState实际上只不过是JavaScript的标准API,它允许我们通过将完整的URL“推送”到浏览器的URL中来操纵浏览器历史记录,而无需往返服务器,并响应URL中的更改使用Javascript - 所有都不使用URL哈希片段。

but when I use push state it does actually makes a server request and expects server to deliver contents under /foo/bar . 但是当我使用push状态时,它实际上发出了服务器请求,并期望服务器在/ foo / bar下传递内容。 I don't understand how I can avoid that. 我不明白我怎么能避免这种情况。

Now let's assume that even with push state, your client is going to make a server request under mydomain.com/foo/bar when you visit this URL directly. 现在让我们假设即使使用推送状态,当您直接访问此URL时,您的客户端将在mydomain.com/foo/bar下发出服务器请求。 In that case, since I'm serving the default HTML file, and this default HTML file has links to scripts in it: 在这种情况下,由于我正在提供默认的HTML文件,并且此默认HTML文件包含指向其中脚本的链接:

<script type="text/javascript" src="/scripts/myscript.js" ></script>

When this HTML loads, it starts looking for scripts under /foo directory instead of root since the server was requested under /foo which obviously does not exist. 当这个HTML加载时,它开始在/ foo目录而不是root下查找脚本,因为服务器是在/ foo下请求的,这显然不存在。 How do I fix this? 我该如何解决?

I'm really confused at this point. 我在这一点上真的很困惑。 I'd like to know how URL routing is usually done in a single page application. 我想知道URL路由通常是如何在单个页面应用程序中完成的。 Any help will be greatly appreciated. 任何帮助将不胜感激。 You can also refer to this other question I have posted about the same issue: Backbone Router : Get rid of # in the URL 您还可以参考我发布的有关同一问题的其他问题: Backbone Router:在URL中删除#

The solution you're trying to implement is very interesting but not that simple. 您尝试实施的解决方案非常有趣,但并非如此简单。 When your server gets a request to mydomain.com/foo/bar , you should redirect to your root with some parameter that the frontend (JavaScript) app can pick-up to know what the original request was for. 当您的服务器收到对mydomain.com/foo/bar的请求时,您应该使用前端(JavaScript)应用程序可以获取的一些参数重定向到您的根目录,以了解原始请求的用途。 For example: 例如:

  1. Client sends GET http://mydomain.com/foo/bar 客户端发送GET http://mydomain.com/foo/bar
  2. Server redirects (responds 302 with Location header set) to http://mydomain.com/#!/foo/bar 服务器重定向(使用Location标头集响应302)到http://mydomain.com/#!/foo/bar
  3. Your SPA is loaded in the browser, and on startup you check for the hash and find #!/foo/bar , so you remove the hash and trigger the /foo/bar route (that's a push-state). 您的SPA在浏览器中加载,并在启动时检查哈希并找到#!/foo/bar ,因此您删除哈希并触发/foo/bar路由(这是推送状态)。 Your resulting URL is again http://mydomain.com/foo/bar : the original URL the user browsed to. 您生成的URL再次是http://mydomain.com/foo/bar :用户浏览的原始网址。

Grooveshark does something similar to this, though it actually responds with a page to the request sent in 1., which does the hash replacement in the client and then sends another request to the server. Grooveshark做了类似的事情,虽然它实际上响应了1中发送的请求的页面,它在客户端中进行哈希替换,然后向服务器发送另一个请求。 It looks unnecessary to me, maybe I'm overlooking something. 对我来说看起来没必要,也许我忽略了一些东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM