Skip to content

Commit

Permalink
Merge pull request #9332 from nestjs/fix/middleware-with-exclude-routes
Browse files Browse the repository at this point in the history
fix(core): apply global middleware to routes excluded from prefix
  • Loading branch information
kamilmysliwiec authored Mar 14, 2022
2 parents 2f4e9d9 + 1ae72e1 commit 8c705aa
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ describe('Global prefix', () => {
server = app.getHttpServer();
await app.init();

await request(server).get('/hello/foo').expect(200);
await request(server)
.get('/hello/foo')
.expect(200, 'Hello: Data attached in middleware');

await request(server)
.get('/middleware/foo')
.expect(200, MIDDLEWARE_PARAM_VALUE);

await request(server)
.get('/api/v1/middleware/foo')
.expect(404);
await request(server).get('/api/v1/middleware/foo').expect(404);
});

afterEach(async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Controller, Get, Post } from '@nestjs/common';
import { Controller, Get, Post, Req } from '@nestjs/common';

@Controller()
export class AppController {
@Get('hello/:name')
getHello(): string {
return 'hello';
getHello(@Req() req): string {
return 'Hello: ' + req.extras?.data;
}

@Get('health')
Expand Down
7 changes: 6 additions & 1 deletion integration/nest-application/global-prefix/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export class AppModule {
.apply((req, res, next) => res.end(MIDDLEWARE_PARAM_VALUE))
.forRoutes({ path: MIDDLEWARE_VALUE + '/*', method: RequestMethod.GET })
.apply((req, res, next) => res.status(201).end(MIDDLEWARE_PARAM_VALUE))
.forRoutes({ path: MIDDLEWARE_VALUE + '/*', method: RequestMethod.POST });
.forRoutes({ path: MIDDLEWARE_VALUE + '/*', method: RequestMethod.POST })
.apply((req, res, next) => {
req.extras = { data: 'Data attached in middleware' };
next();
})
.forRoutes({ path: '*', method: RequestMethod.GET });
}
}
7 changes: 4 additions & 3 deletions packages/core/middleware/middleware-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
addLeadingSlash,
isUndefined,
} from '@nestjs/common/utils/shared.utils';
import { isRouteExcluded, isRequestMethodAll } from '../router/utils';
import { ApplicationConfig } from '../application-config';
import { InvalidMiddlewareException } from '../errors/exceptions/invalid-middleware.exception';
import { RuntimeException } from '../errors/exceptions/runtime.exception';
Expand All @@ -23,6 +22,7 @@ import { InstanceToken, Module } from '../injector/module';
import { REQUEST_CONTEXT_ID } from '../router/request/request-constants';
import { RouterExceptionFilters } from '../router/router-exception-filters';
import { RouterProxy } from '../router/router-proxy';
import { isRequestMethodAll, isRouteExcluded } from '../router/utils';
import { MiddlewareBuilder } from './builder';
import { MiddlewareContainer } from './container';
import { MiddlewareResolver } from './resolver';
Expand Down Expand Up @@ -274,8 +274,9 @@ export class MiddlewareModule {
const prefix = this.config.getGlobalPrefix();
const excludedRoutes = this.config.getGlobalPrefixOptions().exclude;
if (
Array.isArray(excludedRoutes) &&
isRouteExcluded(excludedRoutes, path, method)
(Array.isArray(excludedRoutes) &&
isRouteExcluded(excludedRoutes, path, method)) ||
['*', '/*', '(.*)', '/(.*)'].includes(path)
) {
path = addLeadingSlash(path);
} else {
Expand Down

0 comments on commit 8c705aa

Please sign in to comment.