考虑使用 Node.js Express 服务器的 Backbone.js PushState 路由?
pushState
支持是在 Backbone.js 的 0.5 版更新中引入的。
来自主干文档:
请注意,使用真实 URL 需要您的网络服务器能够 正确呈现这些页面,因此需要进行后端更改,如下所示 出色地。例如,如果您的路由为 /documents/100,则您的网站 如果浏览器访问该 URL,服务器必须能够提供该页面 直接地。为了获得完整的搜索引擎爬行能力,最好有 服务器为页面生成完整的 HTML ...但如果它是一个 Web 应用程序,只需呈现与您将拥有的相同内容 根 URL,并使用 Backbone Views 和 JavaScript 填充其余部分 工作正常。
这似乎是一个微不足道的问题,但我想知道是否有人可以帮助我解决一些具体问题(最好是 express)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
pushState
support was introduced with Backbone.js' version 0.5 update.
From the backbone documentation:
Note that using real URLs requires your web server to be able to
correctly render those pages, so back-end changes are required as
well. For example, if you have a route of /documents/100, your web
server must be able to serve that page, if the browser visits that URL
directly. For full search-engine crawlability, it's best to have the
server generate the complete HTML for the page ... but if it's a web
application, just rendering the same content you would have for the
root URL, and filling in the rest with Backbone Views and JavaScript
works fine.
This may seem like a trivial question, but I'm wondering if anyone can help me with some specific (preferably express) node.js server code. How should I go about handling these routes? I'm a little confused.
Here's the relevant excerpt from my app's router module:
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
};
I only have a top-level route and a route for an about page here. So how should I set up a node server that will allow me to navigate to:
domain.com
domain.com/about
domain.com/#/about // <- for browsers that don't support pushState
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
说明
首先,您需要知道
domain.com/#/about
将调用您服务器的“/”路由,因为它不读取 # 片段。您的服务器将呈现 Backbone.js 应用程序的基础,并且 Backbone 将触发“about”路由。因此,您需要在 Express JS 中声明两条路由:
Code
我向您推荐 Derick Bailey 提供的 3 个与 Backbone.js 兼容的 SEO 链接:
Explanation
First, you need to know that
domain.com/#/about
will call the '/' route of your server because it doesn't read the # fragment. Your server will render the base of your Backbone.js application and Backbone will trigger the 'about' route.So, you need to declare two routes in Express JS:
Code
I recommend you 3 links for SEO compatibility with Backbone.js by Derick Bailey: