Skip to content

Commit

Permalink
allow removing items by path
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Rodriguez committed Jan 10, 2013
1 parent a8c9bde commit 3b79aab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
9 changes: 5 additions & 4 deletions lib/middler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
22 changes: 15 additions & 7 deletions test/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down

0 comments on commit 3b79aab

Please sign in to comment.