Permalink
Browse files

allow removing items by path

  • Loading branch information...
1 parent a8c9bde commit 3b79aab7a57d65049e765d336881978e0fd3de8b Carlos Rodriguez committed Jan 10, 2013
Showing with 20 additions and 11 deletions.
  1. +5 −4 lib/middler.js
  2. +15 −7 test/routing.js
View
@@ -96,15 +96,16 @@ Middler.prototype.add = function addItem () {
this.items.push(new Item(args));
this.sort();
return this;
-}
+};
/**
* Removes item from the stack.
*/
-Middler.prototype.remove = function (fn) {
+Middler.prototype.remove = function (fnOrPath) {
+ var key = typeof fnOrPath === 'function' ? 'fns' : 'paths';
for (var idx in this.items) {
- for (var idx2 in this.items[idx].fns) {
- if (this.items[idx].fns[idx2] === fn) {
+ for (var idx2 in this.items[idx][key]) {
+ if (this.items[idx][key][idx2] === fnOrPath) {
this.items.splice(idx, 1);
return this;
}
View
@@ -77,6 +77,21 @@ describe('routing', function () {
});
});
+ it('can use unnamed params', function (done) {
+ request.put(baseUrl + '/services/foo/bar', function (res) {
+ assert.deepEqual(res.body, ['foo', 'bar']);
+ done();
+ });
+ });
+
+ it('can remove a middleware by path', function (done) {
+ middler(server).remove('/services/*/*');
+ request.get(baseUrl + '/services/foo/bar', function (res) {
+ assertRes(res, 'not found', 404);
+ done();
+ });
+ });
+
it('can add a middleware to run first', function (done) {
middler(server).first('/posts', function (req, res, next) {
writeRes(res, 'whoa!', 500);
@@ -87,13 +102,6 @@ describe('routing', function () {
});
});
- it('can use unnamed params', function (done) {
- request.put(baseUrl + '/services/foo/bar', function (res) {
- assert.deepEqual(res.body, ['foo', 'bar']);
- done();
- });
- });
-
it('closes the server', function (done) {
server.once('close', done);
server.close();

0 comments on commit 3b79aab

Please sign in to comment.