如何在 nest js 中使用 fastify-adapter 配置速率限制 [英] How to configure rate-limit with fastify-adapter in nest js

查看:270
本文介绍了如何在 nest js 中使用 fastify-adapter 配置速率限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始实现 API 的 Nest js,我正在使用 Fastify 适配器.我需要帮助在 Nest JS 中使用 FastifyAdapter 配置速率限制.

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 to the above code and correct the mistake

推荐答案

安装:

npm install fastify-rate-limit --save

导入(在 main.ts 中):

Import (In main.ts):

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

用法:

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'
  });
}

这篇关于如何在 nest js 中使用 fastify-adapter 配置速率限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆