简体   繁体   English

有没有办法对 express nodejs 中的每个 api 键使用不同的速率限制?

[英]is there a way to use different rate limit for each api key in express nodejs?

is there a way to use different rate limit for each api key in express nodejs?有没有办法对 express nodejs 中的每个 api 键使用不同的速率限制?

for example api key "guest" has a rate limit of 500 requests per day and api key "key" has a rate limit of 1000 requests per day.例如,api 键“guest”的速率限制为每天 500 个请求,api 键“key”的速率限制为每天 1000 个请求。

the simple solution is to use an npm package called express-rate-limit , it's a popular one and express recommend to use it.简单的解决方案是使用名为express-rate-limit的 npm package ,这是一种流行的,并且明确推荐使用它。

If the package doesn't work for you, it can be implemented using redis , redis is a memory database, so you can create a record with a sliding time. If the package doesn't work for you, it can be implemented using redis , redis is a memory database, so you can create a record with a sliding time. When a request come in you can upadte or create the record, if the limit was reached you block the request, that logic can be implemented in a middleware.当请求进来时,您可以更新或创建记录,如果达到限制,您会阻止请求,该逻辑可以在中间件中实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM