Added app.remove.VERB()
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 251175c · Browse files · 3 files·+54 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+54 added−5 removed
- tests:+21 added−0 removed
- other:+33 added−5 removed
Provenance
- Commit
b9596d7- Tree
d8c205b- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
251175c
3 files changed+54 added−5 removed
modified:lib/router/index.js+26 added-5 removed
159 unmodified lines160 }161 })();162 };163 164- router.remove = function(path, method){165- var fns = router.lookup(path, method);166- fns.forEach(function(fn){167- routes[fn.method].splice(fn.index, 1);168- });164+ router.remove = function(path, method, ret){165+ var ret = ret || []166+ , route;167+168+ // method specific remove169+ if (method) {170+ method = method.toUpperCase();171+ if (routes[method]) {172+ for (var i = 0; i < routes[method].length; ++i) {173+ route = routes[method][i];174+ if (path == route.path) {175+ route.index = i;176+ routes[method].splice(i, 1);177+ ret.push(route);178+ --i;179+ }180+ }181+ }182+ // global remove183+ } else {184+ _methods.forEach(function(method){185+ router.remove(path, method, ret);186+ });187+ }188+189+ return ret;190 };191 192 router.lookup = function(path, method, ret){193 ret = ret || [];136 unmodified linesmodified:lib/http.js+7 added
56 unmodified lines57 app.init = function(middleware){58 var self = this;59 this.cache = {};60 this.match = {};61+ this.remove = {};62 this.lookup = {};63 this.settings = {};64 this.redirects = {};65 this.isCallbacks = {};57 unmodified lines123 ? null124 : method);125 };126 127+ self.remove[method] = function(url){128+ return self.router.remove(url, 'all' == method129+ ? null130+ : method);131+ };132+133 self.lookup[method] = function(path){134 return self.router.lookup(path, 'all' == method135 ? null136 : method);358 unmodified linesmodified:test/router.test.js+21 added
183 unmodified lines184 app.lookup.get('/user/:id/edit').should.have.length(1);185 app.lookup.get('/').should.have.be.empty;186 app.lookup.all('/user/:id').should.have.length(2);187 },188+189+ 'test app.remove': function(){190+ var app = express.createServer();191+ app.get('/user', function(){});192+ app.get('/user', function(){});193+ app.put('/user', function(){});194+ app.get('/user/:id', function(){});195+ app.put('/user/:id', function(){});196+ app.del('/user/:id', function(){});197+198+ app.get('/user').should.have.length(2);199+ var removed = app.remove.get('/user');200+ removed.should.have.length(2);201+202+ var removed = app.remove.get('/user');203+ removed.should.have.length(0);204+ app.get('/user').should.have.length(0);205+206+ app.remove.all('/user/:id').should.have.length(3);207+ app.remove.all('/user/:id').should.have.length(0);208+ },209 210 'test app.match': function(){211 var app = express.createServer();212 app.get('/user', function(){});24 unmodified lines