Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added app.remove.VERB()

  • Loading branch information...
commit b9596d7ce8235f95c384ec94e173a864ba4d57af 1 parent 251175c
@tj tj authored
Showing with 54 additions and 5 deletions.
  1. +7 −0 lib/http.js
  2. +26 −5 lib/router/index.js
  3. +21 −0 test/router.test.js
View
7 lib/http.js
@@ -58,6 +58,7 @@ app.init = function(middleware){
var self = this;
this.cache = {};
this.match = {};
+ this.remove = {};
this.lookup = {};
this.settings = {};
this.redirects = {};
@@ -123,6 +124,12 @@ app.init = function(middleware){
: method);
};
+ self.remove[method] = function(url){
+ return self.router.remove(url, 'all' == method
+ ? null
+ : method);
+ };
+
self.lookup[method] = function(path){
return self.router.lookup(path, 'all' == method
? null
View
31 lib/router/index.js
@@ -161,11 +161,32 @@ function router(fn){
})();
};
- router.remove = function(path, method){
- var fns = router.lookup(path, method);
- fns.forEach(function(fn){
- routes[fn.method].splice(fn.index, 1);
- });
+ router.remove = function(path, method, ret){
+ var ret = ret || []
+ , route;
+
+ // method specific remove
+ if (method) {
+ method = method.toUpperCase();
+ if (routes[method]) {
+ for (var i = 0; i < routes[method].length; ++i) {
+ route = routes[method][i];
+ if (path == route.path) {
+ route.index = i;
+ routes[method].splice(i, 1);
+ ret.push(route);
+ --i;
+ }
+ }
+ }
+ // global remove
+ } else {
+ _methods.forEach(function(method){
+ router.remove(path, method, ret);
+ });
+ }
+
+ return ret;
};
router.lookup = function(path, method, ret){
View
21 test/router.test.js
@@ -185,6 +185,27 @@ module.exports = {
app.lookup.get('/').should.have.be.empty;
app.lookup.all('/user/:id').should.have.length(2);
},
+
+ 'test app.remove': function(){
+ var app = express.createServer();
+ app.get('/user', function(){});
+ app.get('/user', function(){});
+ app.put('/user', function(){});
+ app.get('/user/:id', function(){});
+ app.put('/user/:id', function(){});
+ app.del('/user/:id', function(){});
+
+ app.get('/user').should.have.length(2);
+ var removed = app.remove.get('/user');
+ removed.should.have.length(2);
+
+ var removed = app.remove.get('/user');
+ removed.should.have.length(0);
+ app.get('/user').should.have.length(0);
+
+ app.remove.all('/user/:id').should.have.length(3);
+ app.remove.all('/user/:id').should.have.length(0);
+ },
'test app.match': function(){
var app = express.createServer();
Please sign in to comment.
Something went wrong with that request. Please try again.