diff --git a/Router.js b/Router.js index 30785c3..f3b5ac4 100644 --- a/Router.js +++ b/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'); @@ -8,6 +9,7 @@ var serverSupportedMethods = ["GET", "POST", function Router(){ + this.shouldHandle404s = true; this.shouldThrowExceptions = false; this.routes = {}; @@ -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){ @@ -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 { diff --git a/test/Router.js b/test/Router.js index 2f3e3f7..0416698 100644 --- a/test/Router.js +++ b/test/Router.js @@ -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()