How do I apply fastify rate limiter to a single route in Nest JS?

Viewed 824

I am able to apply a rate limiter to my nest app globally using code similar to the answer of this question. On the fastify rate limiter readme, it is shown that you can apply a rate limiter to a specific route via a config property with the rateLimit object with options. Nest's documentation does not explain how to do this; is it possible in the framework or am I out of luck?

1 Answers

There's no inherent way to add it only to an endpoint, but you could modify the whitelist property function to return true for all routes except the one(s) you want to rate-limit. It could look something like

app.use(rateLimit({
  whitelist: (req, key) => {
    return !limitProtectedRoutes.includes(req.url);
  }),
}));

May need a few more modifications, but the idea is there

Related