diff --git a/src/response.js b/src/response.js index f8798ec9..730b2f3e 100644 --- a/src/response.js +++ b/src/response.js @@ -679,7 +679,8 @@ module.exports = class Response extends Writable { this.send(str); }); - this.app.render(view, options, done); + // use req.app like express does, so mounted sub-apps resolve views with their own settings + this.req.app.render(view, options, done); } cookie(name, value, options) { const opt = {...(options ?? {})}; // create a new ref because we change original object (https://github.com/dimdenGD/ultimate-express/issues/68) diff --git a/src/router.js b/src/router.js index 3846e0cb..9981add5 100644 --- a/src/router.js +++ b/src/router.js @@ -241,7 +241,9 @@ module.exports = class Router extends EventEmitter { [...chainPrefix, ...pathToMount, { ...route, callbacks: [], - keepMount: true + keepMount: true, + // mounted sub-apps become req.app during their dispatch, like express + mountApp: route.callbacks[0].constructor.name === 'Application' ? route.callbacks[0] : undefined }] ); } @@ -495,6 +497,7 @@ module.exports = class Router extends EventEmitter { return false; } // on optimized routes, there can be more routes, so we have to use unoptimized routing and skip until we find route we stopped at + req.app = this; // restore app in case the optimized path swapped it to a mounted sub-app return this._routeRequest(req, res, 0, this._routes, false, skipUntil); } let callbackindex = 0; @@ -507,6 +510,11 @@ module.exports = class Router extends EventEmitter { const strictRouting = this.get('strict routing'); if(route.use) { + if(route.mountApp) { + // optimized chain: normal dispatch swaps req.app when it enters a mounted Application, + // but the compiled mount route has no callback to do it, so swap it here + req.app = route.mountApp; + } req._stack.push(route.path); const fullMountpath = this.getFullMountpath(req); req._opPath = fullMountpath !== EMPTY_REGEX ? req._originalPath.replace(fullMountpath, '') : req._originalPath; diff --git a/tests/parts/subapp/index.ejs b/tests/parts/subapp/index.ejs new file mode 100644 index 00000000..8fdbc0b3 --- /dev/null +++ b/tests/parts/subapp/index.ejs @@ -0,0 +1,2 @@ +