admin管理员组

文章数量:1406951

I have built several Backbone apps and appreciate the client-side code structure and organization. I'm moving into Node development, using Express, and I'm uncertain as to how Express and Backbone can work together in the handling of routes.

I have built several Backbone apps and appreciate the client-side code structure and organization. I'm moving into Node development, using Express, and I'm uncertain as to how Express and Backbone can work together in the handling of routes.

Share edited Dec 17, 2012 at 18:36 AndrewHenderson asked Dec 17, 2012 at 9:17 AndrewHendersonAndrewHenderson 4,9923 gold badges29 silver badges34 bronze badges 1
  • Backbone.Router is for client-side routing, ie: using the html5 history API to change the URL when you update the page via ajax. What are you trying to do? – jordanj77 Commented Dec 17, 2012 at 11:10
Add a ment  | 

3 Answers 3

Reset to default 3

You need to understand that Node and Backbone are independent from each other.

  • Node is for server-side (e.g working with a database, api serving etc. ) .
  • Backbone is a client-side Javascript MVC framework which gives you a structure for organizing your client-side Javascript application. (the application in the browser)

You can have a Backbone application in your client-side and it can hook up to any back-end either Node, Rails, PHP etc...

For more info check MVVM pattern and Javascript frameworks on the client-side.

http://backbonetutorials./why-would-you-use-backbone/

http://addyosmani./blog/understanding-mvvm-a-guide-for-javascript-developers/

A friend gave me the answer:

Backbone uses hash routes. For instance http://yoursite./#foo

Express will use the traditional http://yoursite./foo

You can use the routers independent of one another based one which address you path to - a hash route for client-side functions and the traditional route for server side functionality.

Both routers can coexist.

Your question on how Backbone and Express can work together cannot really be answered precisely, because there are really countless ways on which they can work together. Hopefully some of the info below can help you do what you want to do.

First of all, you can use www.example./foo (no #) routes on the client side (Backbone) - see the pushState option in Backbone.history.start() documentation. It is possible to integrate the routes on the client side and on the server side. It is not easy to find exactly how to do, though.

Some of the info under those links may help you:

  • https://github./developmentseed/bones
  • https://github./SC5/backbone-serverside
  • https://github./tysoncadenhead/backbone-on-express
  • http://nerds.airbnb./weve-launched-our-first-nodejs-app-to-product
  • http://blog.andyet./2011/feb/15/re-using-backbonejs-models-on-the-server-with-node/

You wrote that you have experience with Backbone but you're moving to Node recently so I assume that you are open to other frameworks than only Express. You may consider using eg. restify (in addition to Express) to make a RESTful service which you can integrate with Backbone.

There are also entire frameworks like Derby or Meteor that cover both client side and server side using one code base and you can share much more than just the routers.

(Also, I've just found this year's (2013) HTML5DevConf talk: Surviving Robots and Old Browsers by Server-side Backbone. I haven't watched it yet but it seems very relevant to your problem.)

本文标签: javascriptCan Backbone and Express routers work together in an Express applicationStack Overflow