nest: MiddlewareConsumer routing is broken
Bug Report
Current behavior
The MiddlewareConsumer routing is broken when you define home route: /
All routes mapped to the same path ‘/’
Input Code
Routing module:
export class RoutingModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply((req, res) => res.send('hit path /'))
.forRoutes({ path: '/', method: RequestMethod.GET })
.apply((req, res) => res.send('hit path /test/app'))
.forRoutes({ path: '/test/app', method: RequestMethod.GET })
.apply((req, res) => res.send('hit path /:username'))
.forRoutes({ path: '/:username', method: RequestMethod.GET });
}
}
App module:
import { Module } from '@nestjs/common';
import { RoutingModule } from './routing.module';
@Module({
imports: [RoutingModule],
})
export class AppModule {}
Get request to the ‘/test/app’ and ‘/:username’ prints ‘hit path /’ Feels like ‘/’ defined as ‘*’
Expected behavior
The /test/app and /:username routes should be accessible
Possible Solution
Environment
"@nestjs/common": "^8.0.6",
"@nestjs/core": "^8.0.6",
"@nestjs/platform-express": "^8.0.6"
For Tooling issues:
- Node version: 14
- Platform: Mac/Linux
Others:
This issue is not reproduced in NestJS 7.X.X - started in 8.X.X.
This flow works as expected with the Controllers:
@Controller()
export class AppController {
@Get('/')
homepage(): string {
return 'hit /';
}
@Get('/test/app')
username(): string {
return 'hit /test/app';
}
@Get('/:username')
users(): string {
return 'hit /:username';
}
}
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 28 (16 by maintainers)
@jmcdo29 I don’t think you’re right. If I register two routes, let’s say
/:usernameand/usersso route/usersshould be registered first. When I register just/why it should be last? This route regex is not overriding/something. And as I said in the bug description:This should be fixed in 8.0.11
app.use != app.get post …
http://127.0.0.1:3001/test => hit path /
http://127.0.0.1:3001/test => hit path /test
the bug description:
Current behavior
forRoutes({ path: ‘/’ }, { path: ‘/test/app’ }, { path: ‘/:username’ }) === forRoutes(AppController)
Expected behavior
forRoutes(AppController) should be equal to [{ path: ‘/$’ }, { path: ‘/test/app$’ }, { path: ‘/:username$’ }]
forRoutes({ path: ‘/’ }, { path: ‘/test/app’ }, { path: ‘/:username’ }) should be not equal to forRoutes(AppController)
Wouldn’t this be due to how regex matches work? i.e. if you want to hit just
/then you either need to bind that last or use/$as the regex to signify the end of the string?