From d3af9e999386b1aaaf835935bdf291ed641b5e9b Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Wed, 27 May 2020 17:15:33 +0200 Subject: [PATCH 1/2] feat: removeAllRoutes public method This method allows to clear routes stack. As discussed here: https://github.com/expressjs/express/issues/4296 --- index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/index.js b/index.js index 3608f26..cf106b3 100644 --- a/index.js +++ b/index.js @@ -435,6 +435,16 @@ Router.prototype.process_params = function process_params(layer, called, req, re param() } +/** + * Remove all paths from the current stack + * + * @public + */ + +Router.prototype.removeAllRoutes = function removeAllRoutes() { + this.stack = [] +} + /** * Use the given middleware function, with optional path, defaulting to "/". * From e609cdd509071e2df637bd029ad27381ec3b84f5 Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Wed, 27 May 2020 17:33:01 +0200 Subject: [PATCH 2/2] added test and little docs --- index.js | 4 +++- test/router.js | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index cf106b3..4f5a12c 100644 --- a/index.js +++ b/index.js @@ -436,7 +436,9 @@ Router.prototype.process_params = function process_params(layer, called, req, re } /** - * Remove all paths from the current stack + * Removes all paths from the current stack + * + * Removing routes could be useful when you need to add/remove routes on runtime. * * @public */ 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()