diff --git a/lib/util.js b/lib/util.js index 05e4144..6d24f89 100644 --- a/lib/util.js +++ b/lib/util.js @@ -29,14 +29,20 @@ exports.watchModule = function watchModule(watcher) { } } -function insertMiddleware(server, index, middlewares) { +function insertMiddleware(server, placeHolder, middlewares) { + var index = placeHolder.index; + var path = placeHolder.path; if (middlewares.length == 0) { return; // nothing to do } var stackHolder = server._router || server; var stackCount = stackHolder.stack.length; middlewares.each(function (mw) { - server.use(mw); + if(path){ + server.use(path, mw); + }else{ + server.use(mw); + } }); if (0 <= index && index < stackCount) { // need to reposition inserted items var insertedMiddleware = stackHolder.stack.splice(stackCount); @@ -46,9 +52,14 @@ function insertMiddleware(server, index, middlewares) { function findPlaceholder(server, placeholderName) { var stackHolder = server._router || server; - return stackHolder.stack.findIndex(function (mw) { + var index = stackHolder.stack.findIndex(function (mw) { return mw.handle.name === placeholderName; }); + + return { + index: index; + path: index === -1 ? null : stackHolder.stack[index].regexp + } } function dumpStack(label, server) {