Can Backbone and Express routers work together in an Express application?

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.

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.com/why-would-you-use-backbone/

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

A friend gave me the answer:

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

Express will use the traditional http://yoursite.com/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.com/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:

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.)