我刚刚开始实现 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();
请参考上面的代码并修改错误
Install:
npm install fastify-rate-limit --save
导入(在 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'
});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)