0
0
NestJSframework~10 mins

Rate limiting with throttler in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the ThrottlerModule in a NestJS module.

NestJS
import { Module } from '@nestjs/common';
import { ThrottlerModule } from '@nestjs/throttler';

@Module({
  imports: [ThrottlerModule.[1]({ ttl: 60, limit: 10 })],
})
export class AppModule {}
Drag options to blanks, or click blank then click option'
AforRoot
Bcreate
Cregister
DforRootAsync
Attempts:
3 left
💡 Hint
Common Mistakes
Using forRootAsync without async configuration.
Using register which is not a method of ThrottlerModule.
Using create which does not exist.
2fill in blank
medium

Complete the code to apply the ThrottlerGuard globally in the module using providers.

NestJS
import { Module } from '@nestjs/common';
import { ThrottlerGuard } from '@nestjs/throttler';
import { [1] } from '@nestjs/core';

@Module({
  providers: [
    {
      provide: [1],
      useClass: ThrottlerGuard
    }
  ],
})
export class AppModule {}
Drag options to blanks, or click blank then click option'
AAPP_GUARD
BGuard
CCanActivate
DInjectable
Attempts:
3 left
💡 Hint
Common Mistakes
Importing CanActivate which is an interface, not a token.
Importing Guard which does not exist.
Importing Injectable which is a decorator.
3fill in blank
hard

Fix the error in the controller method to apply rate limiting with @Throttle decorator.

NestJS
import { Controller, Get } from '@nestjs/common';
import { Throttle } from '@nestjs/throttler';

@Controller('cats')
export class CatsController {
  @Get()
  @Throttle([1])
  findAll() {
    return 'This action returns all cats';
  }
}
Drag options to blanks, or click blank then click option'
A10, ttl: 60
B10, 60
C60, 10
D{ limit: 10, ttl: 60 }
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an object instead of two numbers.
Using named parameters inside the decorator.
Passing a single number.
4fill in blank
hard

Fill both blanks to configure ThrottlerModule with a 30-second window and 5 requests limit.

NestJS
import { Module } from '@nestjs/common';
import { ThrottlerModule } from '@nestjs/throttler';

@Module({
  imports: [ThrottlerModule.forRoot({ ttl: [1], limit: [2] })],
})
export class AppModule {}
Drag options to blanks, or click blank then click option'
A30
B5
C60
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping ttl and limit values.
Using default values instead of specified ones.
5fill in blank
hard

Fill all three blanks to create a custom ThrottlerGuard that ignores requests from admin users.

NestJS
import { Injectable, ExecutionContext } from '@nestjs/common';
import { ThrottlerGuard } from '@nestjs/throttler';

@Injectable()
export class [1] extends ThrottlerGuard {
  protected getTracker(req: any): string {
    const user = req.user;
    if (user && user.role === [2]) {
      return [3];
    }
    return super.getTracker(req);
  }
}
Drag options to blanks, or click blank then click option'
ACustomThrottlerGuard
B'admin'
C'skip'
DAdminGuard
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names.
Using role without quotes.
Returning wrong string to skip rate limiting.