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

It didn't apply the desired config to ServeStaticModule module imported from @nestjs/serve-static #576

Closed
mohamadnabikhani opened this issue Jul 14, 2021 · 2 comments

Comments

@mohamadnabikhani
Copy link

I'm submitting a...

[ ] Regression 
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request

Current behavior

import { ServeStaticModule } from '@nestjs/serve-static';
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';

@Module({
    imports: [
        ServeStaticModule.forRoot({
            rootPath: path.join(__dirname, '..', 'statics'),
            serveRoot: '/statics',
        }),
        ThrottlerModule.forRoot({
            ttl: 60,
            limit: 10,
        }),
    ],
    providers: [
         {
             provide: APP_GUARD,
             useClass: ThrottlerGuard,
         },
     ],
})
export class AppModule implements NestModule {...}
  • for urls like example.com/statics/something the desired config of ThrottlerModuleOptions didn't work and I couldn't find documents for using guard in app.useGlobalGuards and test that, but I try below and got Error:
...
    const throttlerGuard = app.select(ThrottlerModule).get(ThrottlerGuard);
    app.useGlobalGuards(throttlerGuard);
...
Error: Nest could not select the given module (it does not exist in current context)
    at NestApplication.select (/root/app/packages/server-template/node_modules/@nestjs/core/nest-application-context.js:49:19)
    at /root/app/packages/server-template/node_modules/@nestjs/core/nest-factory.js:127:40
    at Function.run (/root/app/packages/server-template/node_modules/@nestjs/core/errors/exceptions-zone.js:9:13)
    at Proxy.<anonymous> (/root/app/packages/server-template/node_modules/@nestjs/core/nest-factory.js:126:46)
    at Proxy.<anonymous> (/root/app/packages/server-template/node_modules/@nestjs/core/nest-factory.js:168:54)
    at /root/app/packages/server-template/src/main.ts:38:32
    at Generator.next (<anonymous>)
    at fulfilled (/root/app/packages/server-template/src/main.ts:5:58)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
  • But the desired config applied to other routes of nestjs controller correctly. This problem just happened for routes that ServeStaticModule provides for serving statics files.

  • for solving this issue i have to use express-rate-limit packages, and use its middleware like below code and it works fine:

import RateLimit from 'express-rate-limit';
...
   app.use(
        RateLimit({
            windowMs: 15 * 60 * 1000, 
            max: 1000, 
        }),
    );
...

Expected behavior

It should apply desired config for statics root, too.

Environment

node: v14.16.0
@nestjs/throttler: 2.0.0
@nestjs/core: 7.6.18
@nestjs/platform-express: 7.6.18
@nestjs/common: 7.6.18
@nestjs/serve-static: 2.1.4
- Platform: docker node:lts
@jmcdo29
Copy link
Member

jmcdo29 commented Jul 14, 2021

If you look at the loaders in the serve-static package you'll see that it just uses app.use/app.register instead of having a full Nest context per route. Because of this, guards, interceptors, and other enhancers will not apply to the routes. The update would need to be made over in the nestjs/serve-static package, and there's nothing this package can do about it

@mohamadnabikhani
Copy link
Author

Oh, now I see that in this issue the problem was discussed, and it seemed it hasn't fixed yet. Thanks

@mohamadnabikhani mohamadnabikhani changed the title It not apply the desired config to ServeStaticModule module imported from @nestjs/serve-static It didn't apply the desired config to ServeStaticModule module imported from @nestjs/serve-static Jul 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants