diff --git a/lib/chain.js b/lib/chain.js index cf6942007..9625b13fe 100644 --- a/lib/chain.js +++ b/lib/chain.js @@ -79,13 +79,13 @@ Chain.call = function call(handle, err, req, res, _next) { */ /** - * Extract handlers from a middle instance + * Get handlers of a chain instance * * @memberof Chain * @instance * @returns {Function[]} handlers */ -Chain.prototype.extractHandlers = function extractHandlers() { +Chain.prototype.getHandlers = function getHandlers() { return this.stack.map(function map(stackItem) { return stackItem.handle; }); diff --git a/lib/router.js b/lib/router.js index 80bb79ce6..c7079e4a1 100644 --- a/lib/router.js +++ b/lib/router.js @@ -37,13 +37,12 @@ function Router(options) { this.strict = Boolean(options.strictRouting); this.log = options.log; - this.mounts = {}; this.name = 'RestifyRouter'; - this._anonymusHandlerCounter = 0; + this.mounts = {}; // Internals - this.mounts = {}; - this.findMyWay = new FindMyWay({ + this._anonymusHandlerCounter = 0; + this._findMyWay = new FindMyWay({ defaultRoute: this.defaultRoute.bind(this) }); } @@ -94,7 +93,7 @@ Router.prototype.defaultRoute = function defaultRoute(req, res, next) { // Check for 405 instead of 404 var allowedMethods = http.METHODS.filter(function some(method) { - return self.findMyWay.find(method, pathname); + return self._findMyWay.find(method, pathname); }); if (allowedMethods.length) { @@ -132,7 +131,7 @@ Router.prototype.lookup = function lookup(req, res, next) { // Find find-my-way (fmw) route self._dtraceStart(req); - var fmwRoute = self.findMyWay.find(req.method, url); + var fmwRoute = self._findMyWay.find(req.method, url); self._dtraceEnd(req, res); // Not found @@ -227,7 +226,7 @@ Router.prototype.mount = function mount(opts, handlers) { chain.use(handler); }); - self.findMyWay.on( + self._findMyWay.on( route.method, route.path, function onRoute(req, res, next) { @@ -260,14 +259,15 @@ Router.prototype.unmount = function unmount(name) { var route = this.mounts[name]; - if (route) { - // TODO: revisit - throw new Error('Unmount is not implemented'); - // this.findMyWay.off(route.method, route.path); - // delete this.mounts[name]; + if (!route) { + return; } - return name; + // TODO: revisit + throw new Error('Unmount is not implemented'); + // this._findMyWay.off(route.method, route.path); + // delete this.mounts[name]; + // return route; }; /** @@ -280,7 +280,7 @@ Router.prototype.unmount = function unmount(name) { * @returns {String} stringified router */ Router.prototype.toString = function toString() { - return this.findMyWay.prettyPrint(); + return this._findMyWay.prettyPrint(); }; /** @@ -297,7 +297,7 @@ Router.prototype.getDebugInfo = function getDebugInfo() { name: route.name, method: route.method.toLowerCase(), path: route.path, - handlers: route.chain.extractHandlers() + handlers: route.chain.getHandlers() }; }); }; diff --git a/lib/server.js b/lib/server.js index 363a1d898..7d152a9db 100644 --- a/lib/server.js +++ b/lib/server.js @@ -85,8 +85,6 @@ var PROXY_EVENTS = [ * If provided the following restify server options will be ignored: * spdy, ca, certificate, key, passphrase, rejectUnauthorized, requestCert and * ciphers; however these can all be specified on httpsServerOptions. - * @param {Boolean} [options.strictRouting=false] - If set, Restify - * will treat "/foo" and "/foo/" as different paths. * @example * var restify = require('restify'); * var server = restify.createServer(); @@ -111,7 +109,6 @@ function Server(options) { this.name = options.name; this.handleUncaughtExceptions = options.handleUncaughtExceptions || false; this.router = options.router; - this.routes = {}; this.secure = false; this.socketio = options.socketio || false; this.dtrace = options.dtrace || options.false; @@ -533,17 +530,12 @@ Server.prototype.param = function param(name, fn) { * @instance * @function rm * @throws {TypeError} on bad input. - * @param {String} route - the route name. + * @param {String} routeName - the route name. * @returns {Boolean} true if route was removed, false if not. */ -Server.prototype.rm = function rm(route) { - var r = this.router.unmount(route); - - if (r && this.routes[r]) { - delete this.routes[r]; - } - - return r; +Server.prototype.rm = function rm(routeName) { + var route = this.router.unmount(routeName); + return !!route; }; ///--- Info and debug methods @@ -639,8 +631,8 @@ Server.prototype.getDebugInfo = function getDebugInfo() { // output the actual routes registered with restify var routeInfo = self.router.getDebugInfo(); - var preHandlers = self.preChain.extractHandlers().map(funcNameMapper); - var useHandlers = self.useChain.extractHandlers().map(funcNameMapper); + var preHandlers = self.preChain.getHandlers().map(funcNameMapper); + var useHandlers = self.useChain.getHandlers().map(funcNameMapper); // get each route's handler chain var routes = _.map(routeInfo, function mapValues(route) { @@ -704,7 +696,6 @@ Server.prototype.getDebugInfo = function getDebugInfo() { Server.prototype.toString = function toString() { var LINE_FMT = '\t%s: %s\n'; var SUB_LINE_FMT = '\t\t%s: %s\n'; - var self = this; var str = ''; function handlersToString(arr) { @@ -722,13 +713,22 @@ Server.prototype.toString = function toString() { str += sprintf(LINE_FMT, 'Accepts', this.acceptable.join(', ')); str += sprintf(LINE_FMT, 'Name', this.name); - // TODO: revisit - // str += sprintf(LINE_FMT, 'Pre', handlersToString(this.preChain)); - str += sprintf(LINE_FMT, 'Router', this.router.toString()); + str += sprintf( + LINE_FMT, + 'Pre', + handlersToString(this.preChain.getHandlers()) + ); + str += sprintf(LINE_FMT, 'Router', ''); + this.router + .toString() + .split('\n') + .forEach(function forEach(line) { + str += sprintf('\t\t%s\n', line); + }); str += sprintf(LINE_FMT, 'Routes', ''); - Object.keys(this.routes).forEach(function forEach(k) { - var handlers = handlersToString(self.routes[k]); - str += sprintf(SUB_LINE_FMT, k, handlers); + _.forEach(this.router.mounts, function forEach(route, routeName) { + var handlers = handlersToString(route.chain.getHandlers()); + str += sprintf(SUB_LINE_FMT, routeName, handlers); }); str += sprintf(LINE_FMT, 'Secure', this.secure); str += sprintf(LINE_FMT, 'Url', this.url); diff --git a/test/chain.test.js b/test/chain.test.js index b30bcb9e6..0e75a6820 100644 --- a/test/chain.test.js +++ b/test/chain.test.js @@ -138,11 +138,11 @@ test('count returns with the number of registered handlers', function(t) { t.end(); }); -test('extractHandlers returns with the array of handlers', function(t) { +test('getHandlers returns with the array of handlers', function(t) { var chain = new Chain(); var handlers = [function(req, res, next) {}, function(req, res, next) {}]; chain.use(handlers[0]); chain.use(handlers[1]); - t.deepEqual(chain.extractHandlers(), handlers); + t.deepEqual(chain.getHandlers(), handlers); t.end(); });