Middleware not working with setGlobalPrefix's exclude list
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Current behavior
Middlewares are not applied to routes (that are excluded by setGlobalPrefix) when using Middlewares with app.setGlobalPrefix.
Minimum reproduction code
https://stackblitz.com/edit/nestjs-typescript-starter-nf5fjs?file=src%2Fmain.ts
Steps to reproduce
- npm start
- go to
https://nestjs-typescript-starter-nf5fjs--3000.local.webcontainer.io/api/test
and######################## THIS IS CUSTOM MIDDLEWARE
is printed out in the console - go to
https://nestjs-typescript-starter-nf5fjs--3000.local.webcontainer.io/health-check
and######################## THIS IS CUSTOM MIDDLEWARE
is NOT printed out in the console
Expected behavior
The middleware should be applied to all routes, including those excluded by global prefix.
Package
- I don’t know. Or some 3rd-party package
-
@nestjs/common
-
@nestjs/core
-
@nestjs/microservices
-
@nestjs/platform-express
-
@nestjs/platform-fastify
-
@nestjs/platform-socket.io
-
@nestjs/platform-ws
-
@nestjs/testing
-
@nestjs/websockets
- Other (see below)
Other package
No response
NestJS version
8.2.6
Packages versions
{
"dependencies": {
"@nestjs/common": "^8.1.1",
"@nestjs/core": "^8.1.1",
"@nestjs/platform-express": "^8.1.1",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.4.0"
},
"devDependencies": {
"@nestjs/cli": "^8.1.3",
"@nestjs/schematics": "^8.0.4",
"@nestjs/testing": "^8.1.1",
"@types/express": "^4.17.13",
"@types/jest": "^27.0.2",
"@types/node": "^16.11.1",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^4.29.2",
"@typescript-eslint/parser": "^4.29.2",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1",
"jest": "^27.3.0",
"prettier": "^2.4.1",
"source-map-support": "^0.5.20",
"supertest": "^6.1.6",
"ts-jest": "^27.0.7",
"ts-loader": "^9.2.6",
"ts-node": "^10.3.0",
"tsconfig-paths": "^3.11.0",
"typescript": "^4.4.4"
}
}
Node.js version
16.14.0
In which operating systems have you tested?
- macOS
- Windows
- Linux
Other
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Nestjs exclude path for middleware - Stack Overflow
I solved in my own. I misunderstood this doc paragraph: The characters ?, +, *, and () may be used in a route...
Read more >Global prefix - FAQ | NestJS - A progressive Node.js framework
You can exclude routes from the global prefix using the following construction: app.setGlobalPrefix('v1', { exclude: [{ path: 'health', ...
Read more >How to Build Web APIs with NestJS, Postgres, and Sequelize
TypeScript interfaces are only used for type-checking and they do not compile down into JavaScript code. Installation. Install the NestJs CLI.
Read more >Path Prefix Middleware in Go - Calhoun.io
ServeMux provided by Go's standard libary in order to apply middleware to specific path prefixes (eg /dashboard/*) while ensuring the middleware isn't run ......
Read more >@femike/swagger-protect - npm
Start using @femike/swagger-protect in your project by running `npm i ... Cookie-parser must be import before setup protect middleware.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
As an alternative (if your middleware doesn’t need any dependencies) you can use a functional middleware and bind it globally in your main.ts using
app.use(customMiddleware)
Let’s track this here https://github.com/nestjs/nest/pull/9332