diff --git a/scopes/harmony/express/express.main.runtime.ts b/scopes/harmony/express/express.main.runtime.ts index e6da38ce9fa3..8751948fe4c4 100644 --- a/scopes/harmony/express/express.main.runtime.ts +++ b/scopes/harmony/express/express.main.runtime.ts @@ -94,11 +94,13 @@ export class ExpressMain { }); if (!options?.disableBodyParser) this.bodyParser(app); - this.middlewareSlot - .toArray() - .flatMap(([, middlewares]) => - middlewares.flatMap((middlewareManifest) => app.use(middlewareManifest.middleware)) - ); + const middlewaresSlot = this.middlewareSlot.values().flat(); + middlewaresSlot.forEach(({ route, middleware }) => { + if (!route) app.use(middleware); + // eslint-disable-next-line @typescript-eslint/no-misused-promises + if (route) app.use(route, middleware); + }); + sortedRoutes.forEach((routeInfo) => { const { method, path, middlewares, disableNamespace } = routeInfo; // TODO: @guy make sure to support single middleware here. diff --git a/scopes/harmony/express/index.ts b/scopes/harmony/express/index.ts index 28a8008f2082..6db4e6dd7afd 100644 --- a/scopes/harmony/express/index.ts +++ b/scopes/harmony/express/index.ts @@ -1,5 +1,6 @@ export { RouteSlot } from './express.main.runtime'; export { Route, Verb } from './types'; -export { Request, Response, NextFunction } from './types'; +export { Request, Response, NextFunction, Middleware } from './types'; +export type { MiddlewareManifest } from './middleware-manifest'; export type { ExpressMain } from './express.main.runtime'; export { ExpressAspect } from './express.aspect'; diff --git a/scopes/harmony/express/middleware-manifest.ts b/scopes/harmony/express/middleware-manifest.ts index 9334f21e574e..a4691276262b 100644 --- a/scopes/harmony/express/middleware-manifest.ts +++ b/scopes/harmony/express/middleware-manifest.ts @@ -1,5 +1,6 @@ import { Middleware } from './types'; export interface MiddlewareManifest { + route?: string; middleware: Middleware; } diff --git a/scopes/harmony/express/types/route.ts b/scopes/harmony/express/types/route.ts index bd05211c6c76..d71a75648a56 100644 --- a/scopes/harmony/express/types/route.ts +++ b/scopes/harmony/express/types/route.ts @@ -5,7 +5,7 @@ import { Response } from './response'; /** * define express Middleware */ -export type Middleware = (req: Request, res: Response, next: NextFunction) => Promise; +export type Middleware = (req: Request, res: Response, next: NextFunction) => void | Promise; export enum Verb { WRITE = 'write',