我刚刚开始实现API的Nest js,并且我正在使用Fastify适配器。我需要帮助以在Nest JS中使用FastifyAdapter配置速率限制。
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();
请参考上面的代码并纠正错误
安装:
npm install fastify-rate-limit --save
导入(在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'
});
}