question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Middleware not working with setGlobalPrefix's exclude list

See original GitHub issue

Is 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

  1. npm start
  2. go to https://nestjs-typescript-starter-nf5fjs--3000.local.webcontainer.io/api/test and ######################## THIS IS CUSTOM MIDDLEWARE is printed out in the console
  3. 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

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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
yn4v4scommented, Feb 15, 2022

Thanks @yn4v4s for you reply. Yup it do work if we add in the controllers manually. However, having a .forRoutes('*') will be much more convenient as there is no need to add in every time a new controller is created.

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)

0reactions
kamilmysliwieccommented, Mar 14, 2022
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found