Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): fix global prefix not working #10801

Merged
merged 2 commits into from
Feb 1, 2023

Conversation

zanminkian
Copy link
Contributor

@zanminkian zanminkian commented Jan 3, 2023

PR #9332 fixes #9124, but will introduce a bug - the global prefix will not work if middleware register in '*' route. For example, run the code below and visit http://localhost:3000/test should not get {hello: 'hi'} response, but actually will get. This means setGlobalPrefix not work.

import { NestFactory } from '@nestjs/core';
import { Injectable, MiddlewareConsumer, Module, NestMiddleware, NestModule } from "@nestjs/common";
import type { Request, Response, NextFunction } from 'express';

@Injectable()
export class EngineMiddleware implements NestMiddleware {
    use(_req: Request, res: Response, _next: NextFunction) {
        res.json({hello: 'hi'})
    }
}

@Module({})
export class AppModule implements NestModule {
    configure(consumer: MiddlewareConsumer) {
        consumer
          .apply(EngineMiddleware)
          .forRoutes('*');
      }
}

async function bootstrap(): Promise<void> {
    const app = await NestFactory.create(AppModule);
    app.setGlobalPrefix('/gp')
    app.listen(3000);
}

bootstrap()

This PR reverts #9332 and fixes #9124 and #9776.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Issue Number: #9124 and #9776

What is the new behavior?

Fix them.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@zanminkian
Copy link
Contributor Author

zanminkian commented Jan 3, 2023

BTW, people who face the same problem just like the code above, have to change forRoutes('*') into forRoutes('*/'), which is workaround.

@kamilmysliwiec
Copy link
Member

The build is now failing

image

@zanminkian zanminkian force-pushed the zmj-fix_gloabl_prefix branch from 1a56903 to 5deabef Compare January 4, 2023 10:05
@coveralls
Copy link

Pull Request Test Coverage Report for Build 52907735-c558-46c4-92b9-7e9fb8f7ff77

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 93.404%

Totals Coverage Status
Change from base Build e91ba0bb-da30-4334-a0c2-615d1d5bb062: 0.0%
Covered Lines: 6202
Relevant Lines: 6640

💛 - Coveralls

@zanminkian
Copy link
Contributor Author

zanminkian commented Jan 4, 2023

The build is now failing

Now passed.

};
if (useExcluded && Array.isArray(excludedRoutes)) {
excludedRoutes.forEach(route => {
router(route.path, mw);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use route.path because route.pathRegex is RegExp instead of string. Express support RegExp path. But Fastify do not support RexExp path, which is a pity.

@kamilmysliwiec
Copy link
Member

lgtm

@kamilmysliwiec
Copy link
Member

I had to revert this PR since it caused tests to fail
image

kamilmysliwiec added a commit that referenced this pull request Feb 1, 2023
@zanminkian
Copy link
Contributor Author

I will look into this problem later.

@v-anton
Copy link

v-anton commented Mar 3, 2023

Yes, #9332 introduces a regression - since that we can't exclude the middleware with the format of <global prefix + path>
/api/hello is not working anymore, I have to switch to just /hello

@zanminkian
Copy link
Contributor Author

@v-anton This PR has been reverted unfortunately. Maybe this bug still exist. Do you have any time to submit a bug report or fix it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Middleware not working with setGlobalPrefix's exclude list
4 participants