@@ -179,20 +179,17 @@ class API {
179179 methods . forEach ( ( _method ) => {
180180 // Method must be a string
181181 if ( typeof _method === 'string' ) {
182- // Check for wild card at this level
183- if ( routes [ 'ROUTES' ] [ '*' ] ) {
182+ const wildcardForPathLevel = routes [ i ] == '' ? routes [ 'ROUTES' ] [ '*' ] : routes [ 'ROUTES' ] [ route [ i ] ] [ '*' ] ;
183+ if ( wildcardForPathLevel != null ) {
184184 if (
185- routes [ 'ROUTES' ] [ '*' ] [ 'MIDDLEWARE' ] &&
185+ wildcardForPathLevel [ 'MIDDLEWARE' ] &&
186186 ( route [ i ] !== '*' || _method !== '__MW__' )
187187 ) {
188- _stack [ '*' ] [ method ] = routes [ 'ROUTES' ] [ '*' ] [ 'MIDDLEWARE' ] . stack ;
188+ _stack [ '*' ] [ method ] = wildcardForPathLevel [ 'MIDDLEWARE' ] . stack ;
189189 }
190- if (
191- routes [ 'ROUTES' ] [ '*' ] [ 'METHODS' ] &&
192- routes [ 'ROUTES' ] [ '*' ] [ 'METHODS' ] [ method ]
193- ) {
190+ if ( wildcardForPathLevel ?. [ 'METHODS' ] ?. [ method ] != null ) {
194191 _stack [ 'm' ] [ method ] =
195- routes [ 'ROUTES' ] [ '*' ] [ 'METHODS' ] [ method ] . stack ;
192+ wildcardForPathLevel [ 'METHODS' ] [ method ] . stack ;
196193 }
197194 } // end if wild card
198195
@@ -216,13 +213,12 @@ class API {
216213 : _stack [ '*' ] [ method ]
217214 ? _stack [ '*' ] [ method ] . concat ( stack )
218215 : stack ,
219- // inherited: _stack[method] ? _stack[method] : [],
220216 route : '/' + parsedPath . join ( '/' ) ,
221217 path : '/' + this . _prefix . concat ( parsedPath ) . join ( '/' ) ,
222218 } ;
223219
224- // If mounting middleware
225- if ( method === '__MW__' ) {
220+ const isMountingMiddleware = _method === '__MW__' ;
221+ if ( isMountingMiddleware ) {
226222 // Merge stacks if middleware exists
227223 if ( routes [ 'ROUTES' ] [ route [ i ] ] [ 'MIDDLEWARE' ] ) {
228224 meta . stack =
@@ -234,17 +230,6 @@ class API {
234230 }
235231 // Add/update middleware
236232 routes [ 'ROUTES' ] [ route [ i ] ] [ 'MIDDLEWARE' ] = meta ;
237-
238- // Apply middleware to all child middlware routes
239- // if (route[i] === "*") {
240- // // console.log("APPLY NESTED MIDDLEWARE");
241- // // console.log(JSON.stringify(routes["ROUTES"], null, 2));
242- // Object.keys(routes["ROUTES"]).forEach((nestedRoute) => {
243- // if (nestedRoute != "*") {
244- // console.log(nestedRoute);
245- // }
246- // });
247- // }
248233 } else {
249234 // Create the methods section if it doesn't exist
250235 if ( ! routes [ 'ROUTES' ] [ route [ i ] ] [ 'METHODS' ] )
@@ -266,8 +251,6 @@ class API {
266251 routes [ 'ROUTES' ] [ route [ i ] ] [ 'METHODS' ] [ _method ] = meta ;
267252 } // end else
268253
269- // console.log('STACK:',meta);
270-
271254 // If there's a wild card that's not at the end
272255 } else if ( route [ i ] === '*' ) {
273256 throw new ConfigurationError (
0 commit comments