From abcd7159073a39ed08f99b83017f9ddab0911249 Mon Sep 17 00:00:00 2001 From: Naz Date: Fri, 3 Dec 2021 20:26:51 +0400 Subject: [PATCH] Fixed express app stacking refs https://github.com/TryGhost/Toolbox/issues/152 - Because the root app module was initialized only once per runtime it caused all the express apps to stack on each other causing all sorts of strange behavior when trying to test redirects/vhost mounts etc. Lesson here: be very cautious of how the module is initialized, an explicit function is almost always a better way! --- core/app.js | 12 ++++++++---- core/boot.js | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/core/app.js b/core/app.js index 88152b9c48..3039aaf6f9 100644 --- a/core/app.js +++ b/core/app.js @@ -27,10 +27,14 @@ const maintenanceMiddleware = (req, res, next) => { fs.createReadStream(path.resolve(__dirname, './server/views/maintenance.html')).pipe(res); }; -const rootApp = express('root'); -rootApp.use(sentry.requestHandler); +const rootApp = () => { + const app = express('root'); + app.use(sentry.requestHandler); -rootApp.enable('maintenance'); -rootApp.use(maintenanceMiddleware); + app.enable('maintenance'); + app.use(maintenanceMiddleware); + + return app; +}; module.exports = rootApp; diff --git a/core/boot.js b/core/boot.js index 4c18f0281f..aea945f6b7 100644 --- a/core/boot.js +++ b/core/boot.js @@ -371,7 +371,7 @@ async function bootGhost({backend = true, frontend = true, server = true} = {}) // Step 2 - Start server with minimal app in global maintenance mode debug('Begin: load server + minimal app'); - const rootApp = require('./app'); + const rootApp = require('./app')(); if (server) { const GhostServer = require('./server/ghost-server');