Skip to content

Commit

Permalink
Merge pull request #3 from omsmith/custom-default-handler
Browse files Browse the repository at this point in the history
feat(default-handler) allow user to specify a default request handler
  • Loading branch information
omsmith committed May 5, 2015
2 parents cccdbdb + e3e73cd commit 955fc61
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
24 changes: 16 additions & 8 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
};
41 changes: 36 additions & 5 deletions spec/empty_spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 955fc61

Please sign in to comment.