Skip to content

Commit d703bff

Browse files
author
kamil.mysliwiec
committed
fix(@nestjs/core) middleware not being hit for controllers without path (nestjs#71)
1 parent 8747519 commit d703bff

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

example/modules/users/auth.middleware.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { NestMiddleware } from '../../../src/common/interfaces/middlewares/nest-
77
export class AuthMiddleware implements NestMiddleware {
88
constructor(private usersService: UsersService) {}
99

10-
public resolve() {
10+
public resolve(): (req, res, next) => void {
1111
return (req, res, next) => {
1212
const username = req.headers['x-access-token'];
1313
const users = this.usersService.getUsers();
@@ -19,6 +19,5 @@ export class AuthMiddleware implements NestMiddleware {
1919
next();
2020
};
2121
}
22-
2322
}
2423

src/core/middlewares/routes-mapper.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class RoutesMapper {
1616
}
1717
const paths = this.routerExplorer.scanForPaths(Object.create(routeMetatype), routeMetatype.prototype);
1818
return paths.map((route) => ({
19-
path: this.validateRoutePath(routePath) + this.validateRoutePath(route.path),
19+
path: this.validateGlobalPath(routePath) + this.validateRoutePath(route.path),
2020
method: route.requestMethod,
2121
}));
2222
}
@@ -32,6 +32,11 @@ export class RoutesMapper {
3232
};
3333
}
3434

35+
private validateGlobalPath(path: string): string {
36+
const prefix = validatePath(path);
37+
return prefix === '/' ? '' : prefix;
38+
}
39+
3540
private validateRoutePath(path: string): string {
3641
return validatePath(path);
3742
}

0 commit comments

Comments
 (0)