From e3e73cd487874afa0440687ae21cbda6f94d533d Mon Sep 17 00:00:00 2001 From: Owen Smith Date: Tue, 5 May 2015 10:43:23 -0400 Subject: [PATCH] feat(default-handler) allow user to specify a default request handler --- lib/index.js | 5 +++++ lib/router.js | 24 ++++++++++++++++-------- spec/empty_spec.js | 41 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 57 insertions(+), 13 deletions(-) diff --git a/lib/index.js b/lib/index.js index 5acec19..b18bca9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -16,6 +16,11 @@ PathRouter.prototype.when = function () { return this; }; +PathRouter.prototype.default = function () { + this._router.default.apply(this._router, arguments); + return this; +}; + PathRouter.prototype.dispatch = function () { this._router.dispatch.apply(this._router, arguments); }; diff --git a/lib/router.js b/lib/router.js index cd1515e..b5e0e4c 100644 --- a/lib/router.js +++ b/lib/router.js @@ -4,9 +4,15 @@ var url = require('fast-url-parser'); module.exports = Router; +function defaultHandler (req, res) { + res.statusCode = 404; + res.end(); +} + function Router () { this._routes = []; this._handlers = {}; + this._default = defaultHandler; } Router.prototype.when = function (route, handler) { @@ -43,6 +49,14 @@ Router.prototype.when = function (route, handler) { return this; }; +Router.prototype.default = function (handler) { + if ('function' !== typeof handler) { + throw new Error('"handler" must be a function'); + } + + this._default = handler; +}; + Router.prototype.dispatch = function (req, res) { var reqPath = url.parse(req.url).pathname, matchedRoute; @@ -56,12 +70,6 @@ Router.prototype.dispatch = function (req, res) { return false; }); - var handler = this._handlers[matchedRoute]; - if (handler) { - handler(req, res); - return; - } - - res.statusCode = 404; - res.end(); + var handler = this._handlers[matchedRoute] || this._default; + handler(req, res); }; diff --git a/spec/empty_spec.js b/spec/empty_spec.js index 5ca6d6e..fcc3945 100644 --- a/spec/empty_spec.js +++ b/spec/empty_spec.js @@ -1,15 +1,46 @@ -/* global describe, it */ +/* global afterEach, beforeEach, describe, it */ 'use strict'; -var router = new (require('../'))().listen(), - request = require('supertest')(router._server); +var request = require('supertest'); + +var Router = new require('../'); describe('router', function () { - it('should 404 with no routes defined', function (done) { - request + var router; + + beforeEach(function () { + router = new Router().listen(); + }); + + afterEach(function () { + router._server.close(); + }); + + it('should 404 with no routes defined, by default', function (done) { + request(router._server) .get('/') .expect(404) .end(done); }); + + it('should call specified default handler with no routes defined', function (done) { + var handlerCalled = false; + router.default(function (req, res) { + handlerCalled = true; + + res.statusCode = 200; + res.end(); + }); + + request(router._server) + .get('/') + .expect(200) + .expect(function () { + if (!handlerCalled) { + throw new Error('expected custom handler to be called'); + } + }) + .end(done); + }); });