Skip to content

Commit

Permalink
router - better handling of subapp routes
Browse files Browse the repository at this point in the history
  • Loading branch information
mklabs committed Mar 20, 2012
1 parent ae8a422 commit d646153
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
6 changes: 3 additions & 3 deletions lib/backnode/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ app.use = function(route, fn){
app.router = function _router(route, router) {
if(!(router instanceof Router)) throw new Error('app.router must be called with a Backbone.Router');

// give it the app mount-point
router.root = new RegExp('^' + route);
// update router's routes to contain the app prefix (for subapp)
router.prefix(route);

connect.proto.use.call(this, route, function(req, res, next) {
Router.handler.handle(route, req, function(e, handler) {
Router.handler.handle(router, req, function(e, handler) {
if(e) return next(e);
// no match for this route
if(!handler) return next();
Expand Down
46 changes: 35 additions & 11 deletions lib/backnode/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
var Backbone = require('backbone'),
Events = Backbone.Events,
underscore = _ = require('underscore'),
parse = require('url').parse;
parse = require('url').parse,
join = require('path').join;

var Router = module.exports = Backbone.Router;

Expand Down Expand Up @@ -92,6 +93,30 @@ Router.prototype.middlewares = function middlewares(route, req, res, cb) {
})(stack.shift());
};

// **prefix** each of the routes with appropriate root prefix (app mount-point)
// and update each of the handlers route RegExp. Similar to `_bindRoutes` but add
// routes prefixed with root.
Router.prototype.prefix = function prefix(root) {
if (!this.routes) return;
var routes = [];
for (var route in this.routes) {
routes.unshift([route, this.routes[route]]);
}

var parts, route;
for (var i = 0, l = routes.length; i < l; i++) {
route = routes[i];
parts = route[0].split(' ');
if(parts.length === 2) route[0] = parts[0] + ' ' + this.join(root, parts[1]);
this.route(route[0], route[1], this[route[1]]);
}
};

// **join** paths and normalize
Router.prototype.join = function _join() {
var res = join.apply({}, arguments);
return res.replace(/\\/g, '/');
};

//
// Router Handler - sever-side equivalent of `Backbone.History`
Expand All @@ -112,24 +137,23 @@ _.extend(RouteHandler.prototype, Events, {

// Attempt to match a route for given `req`, testing it against
// `req.url`, `req.method` and `root` mounted-point.
match: function match(root, req) {
if(!req) req = root, root = '/';
match: function match(router, req) {
var url = parse(req.url);
return _.find(this.handlers, function(handler) {
var method = handler.method === req.method;
return method && handler.router.root.test(url.pathname) && handler.route.test(url.pathname);
var method = handler.method === req.method
self = handler.router === router,
route = handler.route.test(url.pathname);

return method && self && route;
});
},

// handler for incoming request, test `req.url` against each
// registered route.
handle: function handle(root, req, next) {
var route = this.match(root, req);
// registered route and routers.
handle: function handle(router, req, next) {
var route = this.match(router, req);
next(null, route);
return this;
}

});



1 change: 0 additions & 1 deletion test/test-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ var TestRouter = backnode.Router.extend({
}
});


app.use('/prefix', new TestRouter);
app.use(new TestRouter);

Expand Down

0 comments on commit d646153

Please sign in to comment.