Skip to content
Browse files

allow a method without a path following it

  • Loading branch information...
1 parent 4883cd3 commit 83d2df5eea0c90408ddc15c6508e3dbc0242f371 @caolan committed Oct 28, 2010
Showing with 38 additions and 3 deletions.
  1. +13 −1 README.md
  2. +3 −2 lib/dispatch.js
  3. +22 −0 test/test-dispatch.js
View
14 README.md
@@ -75,7 +75,19 @@ Its also possible to define methods for URLs:
Just prefix the URL with the http method in uppercase followed by whitespace
and then the path you want to match against. Nested URLs always match the last
-method defined in the tree.
+method defined in the tree. Because of this, you can use the following style for
+matching request methods, if you prefer:
+
+ dispatch({
+ '/test': {
+ GET: function (req, res, next) {
+ ...
+ },
+ POST: function (req, res, next) {
+ ...
+ }
+ }
+ })
A couple of implementation points:
View
5 lib/dispatch.js
@@ -44,10 +44,11 @@ function compileKeys(urls){
* optional method prefix.
*/
function splitURL(url) {
- var method, path, match = /^([A-Z]+)\s+/.exec(url);
+ var method, path, match = /^([A-Z]+)(?:\s+|$)/.exec(url);
if (match) {
method = match[1];
- url = /^[A-Z]+\s+(.*)$/.exec(url)[1];
+ path = /^[A-Z]+\s+(.*)$/.exec(url);
+ url = path ? path[1]: '';
}
return {url: url, method: method};
}
View
22 test/test-dispatch.js
@@ -236,3 +236,25 @@ exports['nested named param'] = function (test) {
}
})(request, 'response', 'next');
};
+
+exports['method as final object'] = function (test) {
+ var request = {url: '/test/etc', method: 'POST'};
+ dispatch({
+ '/test': {
+ GET: function(req, res, next){
+ test.ok(false, 'GET should not be called');
+ },
+ '/etc': {
+ GET: function(req, res, next){
+ test.ok(false, 'GET should not be called');
+ },
+ POST: function(req, res, next){
+ test.equals(req, request);
+ test.equals(res, 'response');
+ test.equals(next, 'next');
+ test.done();
+ },
+ }
+ }
+ })(request, 'response', 'next');
+};

0 comments on commit 83d2df5

Please sign in to comment.
Something went wrong with that request. Please try again.