diff --git a/index.js b/index.js index 3608f26..4f5a12c 100644 --- a/index.js +++ b/index.js @@ -435,6 +435,18 @@ Router.prototype.process_params = function process_params(layer, called, req, re param() } +/** + * Removes all paths from the current stack + * + * Removing routes could be useful when you need to add/remove routes on runtime. + * + * @public + */ + +Router.prototype.removeAllRoutes = function removeAllRoutes() { + this.stack = [] +} + /** * Use the given middleware function, with optional path, defaulting to "/". * diff --git a/test/router.js b/test/router.js index 138f221..be93936 100644 --- a/test/router.js +++ b/test/router.js @@ -35,6 +35,21 @@ describe('Router', function () { router({}, {}, done) }) + describe('.removeAllRoutes()', function () { + it('should remove all routes from the stack', function (done) { + var router = new Router() + + for (var i = 0; i < 10; i++) { + router.get('/thing' + i, helloWorld) + } + + router.removeAllRoutes() + + assert.equal(router.stack.length, 0) + done() + }) + }) + describe('.all(path, fn)', function () { it('should be chainable', function () { var router = new Router()