documentcloud/backbone — pull request #2473 by cyberthom (2013-04-11 12:02:41+00:00)
Title: Allow Router to be used server side

Backbone assumes that History is a singleton which is not the case in
non-browser environments like node.js where multiple requests have to
be handled simultaneously. This commit is not an attempt to rethink how
Backbone handles Controller/Dispatcher logic across different
environments but a merely an improvement that allows using Router and
History on the server side without breaking the existing api.

## Browser (unchanged)
```javascript
Backbone.history.start({silent: true, pushState: true, hashChange: true});
var router = new Router();
// Shared code
// Run route
router.loadUrl('/posts/123');
// Set browser location to /posts/123
router.navigate('/posts/123');
```

## Server (stubbing location)
```javascript
var url = require('url');
var Location = function(req, res) {
_.extend(this, url.parse(req.url), {req: req, res: res});
};
Location.prototype.assign = Location.prototype.replace = function(path) {
this.res.redirect(path);
};

app.get('/home/', function(req, res) {
var location = new Location(req, res);
var history = new Backbone.History({location: location});
var router = new Router({history: history});
// Shared code
// Run route
router.loadUrl('/posts/123');
// Redirect (302 "Found") to /posts/123
router.navigate('/posts/123');
});
```

Let me know your thoughts and if there are any plans on the future direction of this.

```
Took 2803ms to run 753 tests. 753 passed, 0 failed.
passed 4 tests
```