Skip to content

Commit

Permalink
Merge pull request #9 from zerious/master
Browse files Browse the repository at this point in the history
Added app.unuse
  • Loading branch information
zerious committed Nov 4, 2014
2 parents 2f0ef6b + f8ca8b9 commit 55aa763
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 8 deletions.
25 changes: 19 additions & 6 deletions lib/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ module.exports = function Router(protocol) {
// Middlewares are functions that chain asynchronously.
var middlewares = this.middlewares = [];

this.setApp = function setApp(value) {
this.setApp = function (value) {
app = this.app = value;
};

this.setPort = function setPort(port) {
this.setPort = function (port) {
this.port = port;
};

this.setServer = function setServer(server) {
this.setServer = function (server) {
this.server = server;
};

/**
* Add a function to handle a specific HTTP method and URL path.
*/
this.add = function add(method, path, fn) {
this.add = function (method, path, fn) {
method = method.toUpperCase();
path = patternify(path, '^', '$');
var map = routes[method] = routes[method] || [];
Expand All @@ -68,7 +68,7 @@ module.exports = function Router(protocol) {
/**
* Add a middleware, with an optional path.
*/
this.use = function use(path, fn) {
this.use = function (path, fn) {
var middleware;
if (typeof path == 'function') {
middleware = path;
Expand All @@ -87,10 +87,23 @@ module.exports = function Router(protocol) {
return isMatch ? fn.apply(app, arguments) : true;
};
}
middleware.fn = fn;
}
middlewares.push(middleware);
};

/**
* Remove a middleware, regardless of path.
*/
this.unuse = function (fn) {
for (var i = 0; i < middlewares.length; i++) {
var middleware = middlewares[i];
if ((fn == middleware) || (fn == middleware.fn)) {
middlewares.splice(i--, 1);
}
}
};

/**
* Process a request and write to the response.
*/
Expand Down Expand Up @@ -190,4 +203,4 @@ module.exports = function Router(protocol) {

next();
};
};
};
10 changes: 10 additions & 0 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ var proto = Server.prototype = {
return this;
},

/**
* Remove a middleware function from usage.
*/
unuse: function (fn, protocol) {
this.routers(protocol).forEach(function (router) {
router.unuse(fn);
});
return this;
},

/**
* Listen for HTTP/HTTPS requests based on a `config` object.
* For example:
Expand Down
57 changes: 57 additions & 0 deletions test/middlewareTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var http = require('http');
var Server = require('../lib/Server');
var app;

// TODO: Move middleware-related tests here.
describe('Middleware', function () {

before(function () {
app = new Server({http: 9876});
});

after(function () {
app.close();
});

it('gets added with .use()', function (done) {
var count = 0;
app.use('/test', function (request, response, next) {
count++;
next();
});
app.get('/test/ok', function (request, response) {
response.end('ok' + count);
});
http
.get('http://127.0.0.1:9876/test/ok', function (response) {
response.on('data', function (data) {
is('ok1', '' + data);
done();
});
})
.on('error', done);
});

it('gets removed with .unuse()', function (done) {
var count = 0;
var middleFn = function (request, response, next) {
count++;
is.fail('Should not get here!');
next();
};
app.use('/untest', middleFn);
app.unuse(middleFn);
app.get('/untest/ok', function (request, response) {
response.end('ok' + count);
});
http
.get('http://127.0.0.1:9876/untest/ok', function (response) {
response.on('data', function (data) {
is('ok0', '' + data);
done();
});
})
.on('error', done);
});

});
4 changes: 2 additions & 2 deletions za.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ require('./lib/Response');
var Server = require('./lib/Server');

// The API is a function which returns a server.
var za = module.exports = function () {
return new Server();
var za = module.exports = function (config) {
return new Server(config);
};

// Expose the version number, but only load package JSON if a get is performed.
Expand Down

0 comments on commit 55aa763

Please sign in to comment.