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?
Asked
Active
Viewed 1,139 times
2
-
1Why not add your code examples instead of sending people off to multiple links? Your question would be more clearly understood that way. – Mar 03 '20 at 20:27
1 Answers
3
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

Jay McDoniel
- 57,339
- 7
- 135
- 147
-
A little hacky but if I really can't do it the "proper" way this looks good to me! Will accept after giving a little time for other answers. – monoRed Mar 03 '20 at 17:56
-
Yeah, there's no real way to drop down to the fastify instance to register a middleware directly, so this is about as good as you'll get – Jay McDoniel Mar 03 '20 at 18:01