Basic Usage of @fastify/rate-limit
mainTo use the plugin, register it with your Fastify instance. By default, it adds an onRequest hook that checks the client's IP address against a configured timeWindow and max request limit. If the limit is exceeded, the client receives a 429 Too Many Requests response.
import Fastify from 'fastify'
const fastify = Fastify()
await fastify.register(import('@fastify/rate-limit'), {
max: 100,
timeWindow: '1 minute'
})
fastify.get('/', (request, reply) => {
reply.send({ hello: 'world' })
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
console.log('Server listening at http://localhost:3000')
})