Skip to content

Commit 40800d3

Browse files
Merge pull request #13337 from nestjs/fix/global-prefix-midleware-issue
fix(core): middleware is not executed for root route when global prefix is set
2 parents 9dda2cd + 3321f6c commit 40800d3

File tree

7 files changed

+29
-17
lines changed

7 files changed

+29
-17
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ describe('Global prefix', () => {
136136
server = app.getHttpServer();
137137
await app.init();
138138

139-
await request(server).get('/').expect(200, '1');
139+
await request(server)
140+
.get('/')
141+
.expect(200, 'Extras: Data attached in middleware, Count: 1');
140142
await request(server).get('/api/count').expect(200, '2');
141143
});
142144

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class AppController {
2929

3030
@Get()
3131
getHome(@Req() req) {
32-
return req.count;
32+
return 'Extras: ' + req.extras?.data + ', Count: ' + req.count;
3333
}
3434

3535
@Get('count')

packages/core/middleware/middleware-module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,14 @@ export class MiddlewareModule<
190190
for (const metatype of middlewareCollection) {
191191
const collection = middlewareContainer.getMiddlewareCollection(moduleKey);
192192
const instanceWrapper = collection.get(metatype);
193+
193194
if (isUndefined(instanceWrapper)) {
194195
throw new RuntimeException();
195196
}
196197
if (instanceWrapper.isTransient) {
197198
return;
198199
}
200+
199201
this.graphInspector.insertClassNode(
200202
moduleRef,
201203
instanceWrapper,

packages/core/middleware/route-info-path-extractor.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ export class RouteInfoPathExtractor {
3535
if (this.isAWildcard(path)) {
3636
const entries =
3737
versionPaths.length > 0
38-
? versionPaths.map(
39-
versionPath =>
38+
? versionPaths
39+
.map(versionPath => [
40+
this.prefixPath + versionPath + '$',
4041
this.prefixPath + versionPath + addLeadingSlash(path),
41-
)
42-
: [this.prefixPath + addLeadingSlash(path)];
42+
])
43+
.flat()
44+
: this.prefixPath
45+
? [this.prefixPath + '$', this.prefixPath + addLeadingSlash(path)]
46+
: [addLeadingSlash(path)];
4347

4448
return Array.isArray(this.excludedGlobalPrefixRoutes)
4549
? [

packages/core/test/middleware/builder.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ describe('MiddlewareBuilder', () => {
193193
expect(proxy.getExcludedRoutes()).to.be.eql([
194194
{
195195
path,
196-
method: -1 as any,
196+
method: -1,
197197
},
198198
]);
199199
});

packages/core/test/middleware/route-info-path-extractor.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('RouteInfoPathExtractor', () => {
3131
method: RequestMethod.ALL,
3232
version: '1',
3333
}),
34-
).to.eql(['/v1/*']);
34+
).to.eql(['/v1$', '/v1/*']);
3535
});
3636

3737
it(`should return correct paths when set global prefix`, () => {
@@ -42,15 +42,15 @@ describe('RouteInfoPathExtractor', () => {
4242
path: '*',
4343
method: RequestMethod.ALL,
4444
}),
45-
).to.eql(['/api/*']);
45+
).to.eql(['/api$', '/api/*']);
4646

4747
expect(
4848
routeInfoPathExtractor.extractPathsFrom({
4949
path: '*',
5050
method: RequestMethod.ALL,
5151
version: '1',
5252
}),
53-
).to.eql(['/api/v1/*']);
53+
).to.eql(['/api/v1$', '/api/v1/*']);
5454
});
5555

5656
it(`should return correct paths when set global prefix and global prefix options`, () => {
@@ -66,15 +66,15 @@ describe('RouteInfoPathExtractor', () => {
6666
path: '*',
6767
method: RequestMethod.ALL,
6868
}),
69-
).to.eql(['/api/*', '/foo']);
69+
).to.eql(['/api$', '/api/*', '/foo']);
7070

7171
expect(
7272
routeInfoPathExtractor.extractPathsFrom({
7373
path: '*',
7474
method: RequestMethod.ALL,
7575
version: '1',
7676
}),
77-
).to.eql(['/api/v1/*', '/v1/foo']);
77+
).to.eql(['/api/v1$', '/api/v1/*', '/v1/foo']);
7878

7979
expect(
8080
routeInfoPathExtractor.extractPathsFrom({

packages/platform-fastify/adapters/fastify-adapter.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ import {
4747
import * as pathToRegexp from 'path-to-regexp';
4848
// `querystring` is used internally in fastify for registering urlencoded body parser.
4949
import { parse as querystringParse } from 'querystring';
50+
import {
51+
FASTIFY_ROUTE_CONFIG_METADATA,
52+
FASTIFY_ROUTE_CONSTRAINTS_METADATA,
53+
} from '../constants';
5054
import { NestFastifyBodyParserOptions } from '../interfaces';
5155
import {
5256
FastifyStaticOptions,
5357
FastifyViewOptions,
5458
} from '../interfaces/external';
55-
import {
56-
FASTIFY_ROUTE_CONFIG_METADATA,
57-
FASTIFY_ROUTE_CONSTRAINTS_METADATA,
58-
} from '../constants';
5959

6060
type FastifyHttp2SecureOptions<
6161
Server extends http2.Http2SecureServer,
@@ -554,14 +554,18 @@ export class FastifyAdapter<
554554
await this.registerMiddie();
555555
}
556556
return (path: string, callback: Function) => {
557+
const hasEndOfStringCharacter = path.endsWith('$');
558+
path = hasEndOfStringCharacter ? path.slice(0, -1) : path;
559+
557560
let normalizedPath = path.endsWith('/*')
558561
? `${path.slice(0, -1)}(.*)`
559562
: path;
560563

561564
// Fallback to "(.*)" to support plugins like GraphQL
562565
normalizedPath = normalizedPath === '/(.*)' ? '(.*)' : normalizedPath;
563566

564-
const re = pathToRegexp(normalizedPath);
567+
let re = pathToRegexp(normalizedPath);
568+
re = hasEndOfStringCharacter ? new RegExp(re.source + '$', re.flags) : re;
565569

566570
// The following type assertion is valid as we use import('@fastify/middie') rather than require('@fastify/middie')
567571
// ref https://github.com/fastify/middie/pull/55

0 commit comments

Comments
 (0)