How to configure rate-limit with fastify-adapter in nest js

我只是一个虾纸丫 提交于 2021-02-10 20:27:27

问题


I Just started implementing API's Nest js and I am using Fastify adapter. I need help to configure Rate limit using FastifyAdapter in Nest JS.

async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
        AppModule,
        new FastifyAdapter(),
    );

    const limiter = fastifyRateLimit(fastify(), {
        timeWindow: 15 * 60 * 1000, // 15 minutes
        max: 100 // limit each IP to 100 requests per windowMs
    }, (err) => {
    });
    app.use(limiter);
    await app.listen(configService.getPort());
}

bootstrap();

Please refer the above code and correct the mistake


回答1:


Install:

npm install fastify-rate-limit --save

Import (In main.ts):

import * as fastifyRateLimit from 'fastify-rate-limit';

Usage:

async function bootstrap() {
  // Create our app, bootstrap using fastify
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter()
  );

  // Apply rate limiter
  app.register(fastifyRateLimit, {
    max: 25,
    timeWindow: '1 minute'
  });
}


来源:https://stackoverflow.com/questions/58376187/how-to-configure-rate-limit-with-fastify-adapter-in-nest-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!