Browse files

pass next to matched functions

  • Loading branch information...
1 parent 3747bdf commit 72b4c8c974b08e6d39b067e8cf6e81bf0645dda9 @caolan committed Oct 24, 2010
Showing with 39 additions and 32 deletions.
  1. +13 −13 README.md
  2. +1 −1 lib/dispatch.js
  3. +25 −18 test/test-dispatch.js
View
26 README.md
@@ -9,16 +9,16 @@ regular expressions for matching URLs and calling an associated function.
Connect.createServer(
dispatch({
- '/about': function(req, res){
+ '/about': function(req, res, next){
...
},
- '/user': function(req, res){
+ '/user': function(req, res, next){
...
},
- '/user/posts': function(req, res){
+ '/user/posts': function(req, res, next){
...
},
- '/user/posts/(\\w+)': function(req, res, post){
+ '/user/posts/(\\w+)': function(req, res, next, post){
...
}
})
@@ -33,11 +33,11 @@ you see fit:
Connect.createServer(
dispatch({
- '/about': function(req, res){ ... },
+ '/about': function(req, res, next){ ... },
'/user': {
- '/': function(req, res){ ... },
- '/posts': function(req, res){ ... },
- '/posts/(\\w+)': function(req, res, post){ ... }
+ '/': function(req, res, next){ ... },
+ '/posts': function(req, res, next){ ... },
+ '/posts/(\\w+)': function(req, res, next, post){ ... }
}
})
);
@@ -50,7 +50,7 @@ Let's assume that 'user' is actually provided by another module:
Connect.createServer(
dispatch({
- '/about': function(req, res){ ... },
+ '/about': function(req, res, next){ ... },
'/user': require('./user').urls
})
);
@@ -63,8 +63,8 @@ Its also possible to define methods for URLs:
Connect.createServer(
dispatch({
'/user': {
- 'GET /item': function(req, res){ ... },
- 'POST /item': function(req, res){ ... },
+ 'GET /item': function(req, res, next){ ... },
+ 'POST /item': function(req, res, next){ ... },
}
})
);
@@ -92,10 +92,10 @@ prototyping and just getting my ideas down in code:
var server = Connect.createServer(
quip(),
dispatch({
- '/': function(req, res){
+ '/': function(req, res, next){
res.text('hello world!');
},
- '/api': function(req, res){
+ '/api': function(req, res, next){
res.json({hello: 'world'});
}
})
View
2 lib/dispatch.js
@@ -61,7 +61,7 @@ module.exports = function(urls){
var match = x[0].exec(url.parse(req.url).pathname);
if (match) {
if (!x[1] || x[1] === req.method) {
- x[2].apply(null, [req, res].concat(match.slice(1)));
+ x[2].apply(null, [req, res, next].concat(match.slice(1)));
return true;
}
}
View
43 test/test-dispatch.js
@@ -1,12 +1,13 @@
var dispatch = require('dispatch');
exports['simple match'] = function(test){
- test.expect(2);
+ test.expect(3);
var request = {url: '/test', method: 'GET'};
dispatch({
- '/test': function(req, res){
+ '/test': function(req, res, next){
test.equals(req, request);
test.equals(res, 'response');
+ test.equals(next, 'next');
test.done();
}
})(request, 'response', 'next');
@@ -15,7 +16,7 @@ exports['simple match'] = function(test){
exports['no match'] = function(test){
var request = {url: '/abc', method: 'XYZ'};
dispatch({
- '/test': function(req, res){
+ '/test': function(req, res, next){
test.ok(false, 'should not be called');
}
})(request, 'response', function(){
@@ -27,25 +28,27 @@ exports['no match'] = function(test){
exports['regexp match'] = function(test){
var request = {url: '/abc/test123'};
dispatch({
- '/(\\w+)/test\\d*': function(req, res, group){
+ '/(\\w+)/test\\d*': function(req, res, next, group){
test.equals(req, request);
test.equals(res, 'response');
+ test.equals(next, 'next');
test.equals(group, 'abc');
test.done();
}
})(request, 'response', 'next');
};
exports['multiple matches'] = function(test){
- test.expect(3);
+ test.expect(4);
var request = {url: '/abc', method: 'POST'};
dispatch({
- '/(\\w+)/?': function(req, res, group){
+ '/(\\w+)/?': function(req, res, next, group){
test.equals(req, request);
test.equals(res, 'response');
+ test.equals(next, 'next');
test.equals(group, 'abc');
},
- '/(\\w+)': function(req, res, group){
+ '/(\\w+)': function(req, res, next, group){
test.ok(false, 'only first match should be called');
}
})(request, 'response', 'next');
@@ -57,9 +60,10 @@ exports['nested urls'] = function(test){
dispatch({
'/folder': {
'/some/other': {
- '/path': function(req, res){
+ '/path': function(req, res, next){
test.equals(req, request);
test.equals(res, 'response');
+ test.equals(next, 'next');
test.done();
}
}
@@ -72,17 +76,18 @@ exports['nested urls with captured groups'] = function(test){
dispatch({
'/(\\w+)': {
'/(\\w+)': {
- '/(\\w+)': function(req, res, group1, group2, group3){
+ '/(\\w+)': function(req, res, next, group1, group2, group3){
test.equals(req, request);
test.equals(res, 'response');
+ test.equals(next, 'next');
test.equals(group1, 'one');
test.equals(group2, 'two');
test.equals(group3, 'three');
test.done();
}
}
},
- '/one/two/three': function(req, res){
+ '/one/two/three': function(req, res, next){
test.ok(false, 'should not be called, previous key matches');
test.done();
}
@@ -94,12 +99,12 @@ exports['method'] = function (test) {
var call_order = [];
var request = {url: '/test', method: 'GET'};
var handle_req = dispatch({
- 'GET /test': function(req, res){
+ 'GET /test': function(req, res, next){
call_order.push('GET');
test.equals(req, request);
test.equals(res, 'response');
},
- 'POST /test': function(req, res){
+ 'POST /test': function(req, res, next){
call_order.push('POST');
test.equals(req, request);
test.equals(res, 'response');
@@ -121,7 +126,7 @@ exports['nested method'] = function (test) {
var request = {url: '/path/test', method: 'GET'};
var handle_req = dispatch({
'/path': {
- 'GET /test': function(req, res){
+ 'GET /test': function(req, res, next){
test.equals(req, request);
test.equals(res, 'response');
}
@@ -143,7 +148,7 @@ exports['nested already defined method'] = function (test) {
var handle_req = dispatch({
'/path': {
'POST /create': {
- '/item': function(req, res){
+ '/item': function(req, res, next){
test.equals(req, request);
test.equals(res, 'response');
}
@@ -166,7 +171,7 @@ exports['nested redefine previous method'] = function (test) {
var handle_req = dispatch({
'/path': {
'POST /create': {
- 'GET /item': function(req, res){
+ 'GET /item': function(req, res, next){
test.equals(req, request);
test.equals(res, 'response');
}
@@ -183,17 +188,19 @@ exports['nested redefine previous method'] = function (test) {
};
exports['whitespace between method and pattern'] = function (test) {
- test.expect(4);
+ test.expect(6);
var call_order = [];
var request = {url: '/test', method: 'GET'};
var handle_req = dispatch({
- 'GET /test': function(req, res){
+ 'GET /test': function(req, res, next){
test.equals(req, request);
test.equals(res, 'response');
+ test.equals(next, 'next');
},
- 'POST\t/test': function(req, res){
+ 'POST\t/test': function(req, res, next){
test.equals(req, request);
test.equals(res, 'response');
+ test.equals(next, 'next');
}
});
handle_req(request, 'response', 'next');

0 comments on commit 72b4c8c

Please sign in to comment.