Skip to content

Commit 6752144

Browse files
chore: resolve conflicts
2 parents d5e2879 + 81404bb commit 6752144

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

integration/nest-application/global-prefix/e2e/global-prefix.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ describe('Global prefix', () => {
130130
.expect(200, { '0': 'params', tenantId: 'test' });
131131
});
132132

133+
it(`should execute middleware only once`, async () => {
134+
app.setGlobalPrefix('/api', { exclude: ['/'] });
135+
136+
server = app.getHttpServer();
137+
await app.init();
138+
139+
await request(server).get('/').expect(200, '1');
140+
await request(server).get('/api/count').expect(200, '2');
141+
});
142+
133143
afterEach(async () => {
134144
await app.close();
135145
});

integration/nest-application/global-prefix/src/app.controller.ts

+10
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@ export class AppController {
2626
postTest(): string {
2727
return 'test';
2828
}
29+
30+
@Get()
31+
getHome(@Req() req) {
32+
return req.count;
33+
}
34+
35+
@Get('count')
36+
getCount(@Req() req) {
37+
return req.count;
38+
}
2939
}

integration/nest-application/global-prefix/src/app.module.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const MIDDLEWARE_PARAM_VALUE = 'middleware_param';
88
controllers: [AppController],
99
})
1010
export class AppModule {
11+
private count = 0;
1112
configure(consumer: MiddlewareConsumer) {
1213
consumer
1314
.apply((req, res, next) => res.end(MIDDLEWARE_VALUE))
@@ -27,6 +28,12 @@ export class AppModule {
2728
req.middlewareParams = req.params;
2829
next();
2930
})
30-
.forRoutes({ path: '*', method: RequestMethod.GET });
31+
.forRoutes({ path: '*', method: RequestMethod.GET })
32+
.apply((req, res, next) => {
33+
this.count += 1;
34+
req.count = this.count;
35+
next();
36+
})
37+
.forRoutes('*');
3138
}
3239
}

packages/core/middleware/middleware-module.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,11 @@ export class MiddlewareModule<
327327
}
328328
return next();
329329
};
330-
paths.forEach(path => router(path, middlewareFunction));
330+
const pathsToApplyMiddleware = [];
331+
paths.some(path => path.match(/^\/?$/))
332+
? pathsToApplyMiddleware.push('/')
333+
: pathsToApplyMiddleware.push(...paths);
334+
pathsToApplyMiddleware.forEach(path => router(path, middlewareFunction));
331335
}
332336

333337
private getContextId(request: unknown, isTreeDurable: boolean): ContextId {

0 commit comments

Comments
 (0)