Backbone.js pushState路由与node.jsexpression服务器帐户?

在Backbone.js的0.5版更新中引入了pushState支持。

从骨干文件 :

请注意,使用真实URL需要您的Web服务器能够正确呈现这些页面,因此还需要后端更改。 例如,如果路由为/ documents / 100,则如果浏览器直接访问该URL,则您的Web服务器必须能够提供该页面。 对于完整的search引擎可爬行性,最好让服务器为页面生成完整的HTML …但是如果它是一个Web应用程序,只需渲染与根URL相同的内容,然后用Backbone填充剩下的内容视图和JavaScript工作正常。

这可能看起来像一个微不足道的问题,但我想知道是否有人可以帮我一些特定的(最好是expression式 ) node.js服务器代码。 我应该如何去处理这些路线? 我有点困惑。

以下是我的应用路由器模块的相关摘录:

 var Router = Backbone.Router.extend({ routes: { '': 'index', 'about': 'about' }, index: function() { indexView.render(); }, about: function() { aboutView.render(); } }); var initialize = function() { var router = new Router; Backbone.history.start({ pushState: true }); } return { initialize: initialize }; 

在这里,我只有一个关于页面的顶级路线和路线。 那么应该如何设置一个节点服务器来让我导航到:

 domain.com domain.com/about domain.com/#/about // <- for browsers that don't support pushState 

说明

首先,您需要知道domain.com/#/about会调用服务器的“/”路由,因为它不会读取#片段。 您的服务器将呈现Backbone.js应用程序的基础,Backbone将触发“约”路线。

所以,你需要在Express JS中声明两个路由:

  • /
  • /关于

 app.get('/', function(req, res) { // Trigger the routes 'domain.com' and 'domain.com/#/about' // Here render the base of your application }); app.get('/about', function (req, res) { // Trigger the route 'domain.com/about' // Here use templates to generate the right view and render }); 

我build议你3个与Derick Bailey的Backbone.js兼容的链接:

  • search引擎优化和可访问性与HTML5 PushState,第1部分:介绍PushState: http ://lostechies.com/derickbailey/2011/09/26/seo-and-accessibility-with-html5-pushstate-part-1-introducing-pushstate/
  • search引擎优化和可访问性与HTML5 PushState,第2部分:与Backbone.js渐进增强: http ://lostechies.com/derickbailey/2011/09/26/seo-and-accessibility-with-html5-pushstate-part-2-progressive -enhancement-与骨干-JS /
  • search引擎优化和可访问性与HTML5 PushState,第3部分:video: http ://lostechies.com/derickbailey/2011/10/06/seo-and-accessibility-with-html5-pushstate-part-3-the-video/