USC Law rolls out busload of advocacy

The University of South Carolina School of Law has taken its show on the road, using a new, custom-made bus to haul students and volunteer attorneys to remote areas to deliver desperately needed…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Rate Limiting Using Throttler In Nest Js

Rate limiting is used to restrict users to hit an endpoint for a limited amount of time. In other words, by rate limiting, we can control the number of incoming requests per time. As we can define the user can hit an endpoint 10 times every minute. So, if anyone hits that endpoint more than 10 times, then it will throw an error and the user can not access that URL for a limited amount of time.

In this example, we will see how we can use the Throttler package for rate limiting.

Install package:

After the successful installation of the package, you can use ThrottlerModule just like any other module of the nest. You can use this module with forRoot or forRootAsync methods. In your app.module.ts file import module like :

In the above code “ttl” is Time To Live, which means the amount of time at which you want to restrict the requests. And the limit is used for the amount of requests per ttl.

After importing you need to use a guard to use this module globally.
For that you need to do like:

By doing this now your Rate limit is set to 10 Requests per minute for every endpoint of your project.

app.module.ts file will look like this:

Now if you want to Skip some modules from Rate limiting then you need to specify SkipThrottle() function in that controller like:

Now the rate limit will not apply to any of this controller’s route.

If you want to override “ttl” or “limit” which is set globally, then you need to mention that like:

This will override default configurations for this specific route.

APIs Which Are Having Large Data — We can use this where we have large Response data in API. By doing this we can restrict multiple API calls at the same time and it will optimize the server load.

The built in storage is an in memory cache that keeps track of the requests made until they have passed the TTL set by the global options. You can drop in your own storage option to the storage option of the ThrottlerModule so long as the class implements the ThrottlerStorage interface.

Add a comment

Related posts:

Ingredients for making Potato Podimas

Potato Podimas is a typical South Indian dish that goes well with plain rice. You can use these podimas as stuff for samosas and aloo bread sandwiches as well. You can prepare this for your kids’…

Includia Agency

Includia est une startup créative crée en 2014 basé à Douala, spécialisée en stratégie numérique et en production de contenu qui fonde et organise son business model sur son expertise en…

Drawing tools can now sync across all charts of your layout

All charts of your layout had to have the same symbol in order for drawings to sync up — now you can have any symbols you want. Click Sync drawings to all charts and your notes, forecasts, trend…