mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
chore: resolve conflicts
This commit is contained in:
@@ -130,6 +130,16 @@ describe('Global prefix', () => {
|
||||
.expect(200, { '0': 'params', tenantId: 'test' });
|
||||
});
|
||||
|
||||
it(`should execute middleware only once`, async () => {
|
||||
app.setGlobalPrefix('/api', { exclude: ['/'] });
|
||||
|
||||
server = app.getHttpServer();
|
||||
await app.init();
|
||||
|
||||
await request(server).get('/').expect(200, '1');
|
||||
await request(server).get('/api/count').expect(200, '2');
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
@@ -26,4 +26,14 @@ export class AppController {
|
||||
postTest(): string {
|
||||
return 'test';
|
||||
}
|
||||
|
||||
@Get()
|
||||
getHome(@Req() req) {
|
||||
return req.count;
|
||||
}
|
||||
|
||||
@Get('count')
|
||||
getCount(@Req() req) {
|
||||
return req.count;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ export const MIDDLEWARE_PARAM_VALUE = 'middleware_param';
|
||||
controllers: [AppController],
|
||||
})
|
||||
export class AppModule {
|
||||
private count = 0;
|
||||
configure(consumer: MiddlewareConsumer) {
|
||||
consumer
|
||||
.apply((req, res, next) => res.end(MIDDLEWARE_VALUE))
|
||||
@@ -27,6 +28,12 @@ export class AppModule {
|
||||
req.middlewareParams = req.params;
|
||||
next();
|
||||
})
|
||||
.forRoutes({ path: '*', method: RequestMethod.GET });
|
||||
.forRoutes({ path: '*', method: RequestMethod.GET })
|
||||
.apply((req, res, next) => {
|
||||
this.count += 1;
|
||||
req.count = this.count;
|
||||
next();
|
||||
})
|
||||
.forRoutes('*');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -327,7 +327,11 @@ export class MiddlewareModule<
|
||||
}
|
||||
return next();
|
||||
};
|
||||
paths.forEach(path => router(path, middlewareFunction));
|
||||
const pathsToApplyMiddleware = [];
|
||||
paths.some(path => path.match(/^\/?$/))
|
||||
? pathsToApplyMiddleware.push('/')
|
||||
: pathsToApplyMiddleware.push(...paths);
|
||||
pathsToApplyMiddleware.forEach(path => router(path, middlewareFunction));
|
||||
}
|
||||
|
||||
private getContextId(request: unknown, isTreeDurable: boolean): ContextId {
|
||||
|
||||
Reference in New Issue
Block a user