Skip to content

Commit

Permalink
unify interface for router and route, and allow extra parameters
Browse files Browse the repository at this point in the history
passing from the router invocation.
  • Loading branch information
clement committed Jun 30, 2010
1 parent e33e4f4 commit 1739078
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
58 changes: 31 additions & 27 deletions index.js
@@ -1,30 +1,34 @@
var clutch = exports,
url = require('url'),
slice = Array.prototype.slice;
url = require('url');

var Route = function (method, path_re, callback) {
this.method = method;
this.path_re = path_re;
this.callback = callback;
function slice(arrayLike) {
// A slice method that works on pseudo-arrays like
// `arguments` or the result of a regexp match

var sliceMethod = Array.prototype.slice;
return sliceMethod.apply(arrayLike, sliceMethod.apply(arguments, [1]));
}
Route.prototype.match = function (req, resp) {
// Call the route callback with captured parameters
// if the request URL match
//
// Return `true` if the request matched, `false` otherwise

// `*` match on all methods
if (this.method == '*' || this.method.toLowerCase() == req.method.toLowerCase()) {
var parts,
path = url.parse(req.url).pathname;

if (parts = path.match(this.path_re)) {
this.callback.apply(null, slice.apply(arguments).concat(slice.apply(parts, [1])));
return true;

function Route(method, path_re, callback) {
return function (req, resp) {
// Call the route callback with captured parameters
// if the request URL match
//
// Return `true` if the request matched, `false` otherwise

// `*` match on all methods
if (method == '*' || method.toLowerCase() == req.method.toLowerCase()) {
var parts,
path = url.parse(req.url).pathname;

if (parts = path.match(path_re)) {
callback.apply(null, slice(arguments).concat(slice(parts, 1)));
return true;
}
}
}

return false;
return false;
};
}


Expand All @@ -43,13 +47,13 @@ clutch.route = function (urls, req, res) {
throw new Error('invalid URL : `'+urls[i][0]+'`');
}

routes.push(new Route(parts[1], new RegExp('^'+parts[2]), urls[i][1]));
routes.push(Route(parts[1], new RegExp('^'+parts[2]), urls[i][1]));
}

var _route = function(req, res) {
var i;
for (i in routes) {
if (routes[i].match(req, res)) {
if (routes[i].apply(null, arguments)) {
return true;
}
}
Expand All @@ -58,7 +62,7 @@ clutch.route = function (urls, req, res) {
}

if (req && res) {
return _route(req, res);
return _route.apply(null, slice(arguments, 1));
}
else {
return _route;
Expand All @@ -73,15 +77,15 @@ clutch.route404 = function (urls, req, res) {
var router = clutch.route(urls);

var _route = function (req, res) {
if (!router(req, res)) {
if (!router.apply(null, arguments)) {
res.writeHead(404);
res.end();
}
return true;
}

if (req && res) {
return _route(req, res);
return _route.apply(null, slice(arguments, 1));
}
else {
return _route;
Expand Down
10 changes: 9 additions & 1 deletion test.js
Expand Up @@ -97,14 +97,22 @@ function testParams() {
router(new MockRequest('GET', '/foo/bar'), new MockResponse('testParams3', 2, 200, '["foo","/","bar"]'));
}

function testExtraParams() {
var router = clutch.route404([['GET /$', echo(1)],
['GET /(.*)$', echo(2)]]);
router(new MockRequest('GET', '/'), new MockResponse('testExtraParams1', 1, 200, '[42,"bar"]'), 42, 'bar');
router(new MockRequest('GET', '/foo'), new MockResponse('testExtraParams2', 2, 200, '[42,"baz","foo"]'), 42, 'baz');
}

var tests = [
testInvalidRoute,
testBasic,
test404,
testNoRoutes,
testDynamicRoutes,
testPriority,
testParams
testParams,
testExtraParams
];

sys.log('Test suite started');
Expand Down

0 comments on commit 1739078

Please sign in to comment.