diff --git a/packages/server/src/server.js b/packages/server/src/server.js index dab8445934..9dd5c95896 100644 --- a/packages/server/src/server.js +++ b/packages/server/src/server.js @@ -206,6 +206,12 @@ export default class Server { } } + // SubApp (Express) + if (typeof middleware.handle.handle === 'function') { + const server = middleware.handle + middleware.handle = server.handle.bind(server) + } + return middleware } @@ -222,10 +228,7 @@ export default class Server { consola.error('ServerMiddleware Error:', error) // Placeholder for error - middleware = { - route: '#error', - handle: (req, res, next) => { next(error) } - } + middleware = (req, res, next) => { next(error) } } // Normalize @@ -237,10 +240,15 @@ export default class Server { return middleware } - resolveMiddleware (middleware) { + resolveMiddleware (middleware, fallbackRoute = '/') { // Ensure middleware is normalized middleware = this._normalizeMiddleware(middleware) + // Fallback route + if (!middleware.route) { + middleware.route = fallbackRoute + } + // Resolve final route middleware.route = ( (middleware.prefix !== false ? this.options.router.base : '') + @@ -255,7 +263,7 @@ export default class Server { useMiddleware (middleware) { const { route, handle } = this.resolveMiddleware(middleware) - this.app.use(route.includes('#error') ? '/' : route, handle) + this.app.use(route, handle) } replaceMiddleware (query, middleware) { @@ -275,7 +283,7 @@ export default class Server { } // Resolve middleware - const { route, handle } = this.resolveMiddleware(middleware) + const { route, handle } = this.resolveMiddleware(middleware, serverStackItem.route) // Update serverStackItem serverStackItem.handle = handle