Sachin Shah
Sachin Shah

Reputation: 4533

Rate limit feature is not working in nestjs app

I'm exploring the NestJS and I'm trying to explore rate limit feature.

This is my main.ts file.

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as rateLimit from 'express-rate-limit';
async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
  app.use(
    rateLimit({
      windowMs: 60 * 1000, // 1 minutes
      max: 10, // limit each IP to 100 requests per windowMs
    }),
  );
}
bootstrap();

I'm using its official site for reference.

Required output

Note:

Upvotes: 2

Views: 3774

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70412

You need to set the use of rateLimit before you call app.listen(). Once you call app.listen() you can't assign more middleware to be used for the server.

Upvotes: 5

Related Questions