Skip to content

Commit

Permalink
made the router an event emitter for "route" events
Browse files Browse the repository at this point in the history
  • Loading branch information
cainus committed May 28, 2012
1 parent a1c44d1 commit 4a0b695
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Router.js
@@ -1,4 +1,5 @@
var _ = require('underscore');
var events = require('events');
var DetourError = require('./DetourError').DetourError;
var FSRouteLoader = require('./FSRouteLoader').FSRouteLoader;
var url = require('url');
Expand All @@ -8,6 +9,7 @@ var serverSupportedMethods = ["GET", "POST",


function Router(){

this.shouldHandle404s = true;
this.shouldThrowExceptions = false;
this.routes = {};
Expand All @@ -21,6 +23,7 @@ function Router(){

}

Router.prototype = Object.create(events.EventEmitter.prototype);

// cb simply takes an err object. It is called when the directory is done loading.
Router.prototype.routeDirectory = function(dir, cb){
Expand Down Expand Up @@ -285,6 +288,7 @@ Router.prototype.route = function(path, handler){
handler.OPTIONS = function(req, res){ that.handleOPTIONS(req, res); };
}

this.emit("route", handler);
if (isStarPath(path)){
addStarRoute(this, path, { handler : handler, middlewares : []});
} else {
Expand Down
12 changes: 10 additions & 2 deletions test/Router.js
Expand Up @@ -228,14 +228,22 @@ describe('Router', function(){
});

describe('#route', function(){
it ("emits an event on every routed object", function(){
var d = new Router()
d.on("route", function(resource){
should.exist(resource.GET);
_.isFunction(resource.GET).should.equal(true)
});
d.route('/', function(req, res){return "hello world";});
});

it ("can route a function as a GET", function(){
var d = new Router()
d.route('/', function(req, res){return "hello world";});
var req = { url : "http://asdf.com/", method : "GET"}
d.dispatch(req, this.res)
this.res.expectEnd("hello world")

})
});

it ("can route an object with a GET", function(){
var d = new Router()
Expand Down

0 comments on commit 4a0b695

Please sign in to comment.