From 5bd78f5ecc3e6a1e088f5ccb5ddb9197b5e43bad Mon Sep 17 00:00:00 2001 From: Matthijs van Henten Date: Thu, 21 May 2015 23:41:09 +0000 Subject: [PATCH 1/3] adds a mount feature to frontdoor: mount sections into the existing chain --- node_modules/frontdoor/lib/section.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/node_modules/frontdoor/lib/section.js b/node_modules/frontdoor/lib/section.js index 1e8edbfa..9d482dcc 100644 --- a/node_modules/frontdoor/lib/section.js +++ b/node_modules/frontdoor/lib/section.js @@ -70,6 +70,13 @@ module.exports = function Section(name, description, types) { return section; }; + this.mount = function( name, section ){ + if (!sections[name]) + sections[name] = []; + + sections[name].push(section); + }; + this._rootHandler = function(req, res) { this.handle(req, res, function(err) { if (err) { From 4ffa8c0e8ee5eaac7c53d97f40f3a118dc8de4f8 Mon Sep 17 00:00:00 2001 From: Matthijs van Henten Date: Thu, 21 May 2015 23:42:00 +0000 Subject: [PATCH 2/3] adds a global handler to frontdoor: allow injection of global middlewares --- node_modules/frontdoor/lib/section.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/node_modules/frontdoor/lib/section.js b/node_modules/frontdoor/lib/section.js index 9d482dcc..03bb6f17 100644 --- a/node_modules/frontdoor/lib/section.js +++ b/node_modules/frontdoor/lib/section.js @@ -15,6 +15,7 @@ module.exports = function Section(name, description, types) { var self = this; this.middlewares = []; + this.globals = []; this.errorHandlers = []; this.name = name; @@ -45,10 +46,22 @@ module.exports = function Section(name, description, types) { }); }; + /** + * Insert global middleware(s) for this section: The parent section is + * always an empty container that delegates to it's sub-sections, and + * executes all the middelwares for that section. + * + * This method allows us to inject a "global" middleware that is inserted + * *after* decodeParams and *before* all other middelwares. + */ + this.global = function( middleware ){ + this.globals.push.apply(this.globals, flatten(middleware)); + }; + this.use = function(middleware) { this.middlewares.push.apply(this.middlewares, flatten(middleware)); }; - + this.error = function(middleware) { this.errorHandlers.push.apply(this.errorHandlers, flatten(middleware)); }; @@ -112,7 +125,7 @@ module.exports = function Section(name, description, types) { var method = req.method.toLowerCase(); if (methods.indexOf(method) == -1) return next(); - + var handler = this.match(req, path, method); if (!handler) return next(); @@ -125,6 +138,9 @@ module.exports = function Section(name, description, types) { handler = handler.parent; } + if (this.globals.length) + middleware.splice.apply( middleware, [1, 0].concat( this.globals ) ); + var i = 0; function processNext() { handler = middleware[i++]; From 8ba1a02f2994301eeb7992958b96b211e2bc5f4d Mon Sep 17 00:00:00 2001 From: Matthijs van Henten Date: Fri, 22 May 2015 10:34:11 +0000 Subject: [PATCH 3/3] tidied --- node_modules/frontdoor/lib/section.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/node_modules/frontdoor/lib/section.js b/node_modules/frontdoor/lib/section.js index 03bb6f17..1e027ef0 100644 --- a/node_modules/frontdoor/lib/section.js +++ b/node_modules/frontdoor/lib/section.js @@ -83,13 +83,13 @@ module.exports = function Section(name, description, types) { return section; }; - this.mount = function( name, section ){ + this.mount = function(name, section) { if (!sections[name]) sections[name] = []; sections[name].push(section); }; - + this._rootHandler = function(req, res) { this.handle(req, res, function(err) { if (err) {