nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.7k stars 7.63k forks source link

Middleware's exclude and forRoutes behavior inconsistently #9990

Closed myNameIsDu closed 1 year ago

myNameIsDu commented 2 years ago

Is there an existing issue for this?

Current behavior

when use setGlobalPrefix the Middleware's exclude and forRoutes behavior inconsistently.

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-pzhkvx?file=src/app.module.ts

Steps to reproduce

  1. request /api/hello .
  2. Console output execution middleware.
  3. Put comment on /api/hello . Get rid of /hello's comment.
  4. re-execute npm start.
  5. request /api/hello.
  6. Console doen't output execution middleware.
  7. Put comment on forRoutes('/admin/*') comment . Get rid of /api/hello's comment.
  8. re-execute npm start.
  9. request /api/hello.
  10. Console output execution middleware.

Expected behavior

exclude and forRoutes have the same behavior

Package

Other package

No response

NestJS version

9.0.5

Packages versions

{
  "name": "nest-typescript-starter",
  "private": true,
  "version": "1.0.0",
  "description": "Nest TypeScript starter repository",
  "license": "MIT",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^9.0.5",
    "@nestjs/core": "^9.0.5",
    "@nestjs/platform-express": "^9.0.5",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.5.5"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.1",
    "@nestjs/testing": "^9.0.5",
    "@types/express": "^4.17.13",
    "@types/jest": "^28.1.4",
    "@types/node": "^18.0.3",
    "@types/supertest": "^2.0.12",
    "@typescript-eslint/eslint-plugin": "^5.30.5",
    "@typescript-eslint/parser": "^5.30.5",
    "eslint": "^8.19.0",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-prettier": "^4.2.1",
    "jest": "^28.1.2",
    "prettier": "^2.7.1",
    "source-map-support": "^0.5.21",
    "supertest": "^6.2.4",
    "ts-jest": "^28.0.5",
    "ts-loader": "^9.3.1",
    "ts-node": "^10.8.2",
    "tsconfig-paths": "^4.0.0",
    "typescript": "^4.7.4"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Node.js version

v16.14.2

In which operating systems have you tested?

Other

No response

nouratalaat commented 1 year ago

We're facing the same issue here when upgrading to v9, are we doing something wrong? Did you manage to fix this?

myNameIsDu commented 1 year ago

We're facing the same issue here when upgrading to v9, are we doing something wrong? Did you manage to fix this?

I can only add globalPrefix in the exclude

export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(AuthMiddleware)
      .exclude(
        {
          path: `${globalPrefix}/public/xxxxxx`,
          method: RequestMethod.GET,
        },
      )
      .forRoutes('/*');
    consumer.apply(xxxMiddleware).forRoutes('/xxxxx/*');
  }
}
kamilmysliwiec commented 1 year ago

Let's track this here https://github.com/nestjs/nest/pull/11042