Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

rate-limit.js 1.2 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  1. const rateLimit = require('express-rate-limit')
  2. const RedisStore = require('rate-limit-redis')
  3. const createRedisClient = require('../lib/redis/create-client')
  4. const isProduction = process.env.NODE_ENV === 'production'
  5. const { REDIS_URL } = process.env
  6. const rateLimitDatabaseNumber = 0
  7. const EXPIRES_IN_AS_SECONDS = 60
  8. module.exports = rateLimit({
  9. // 1 minute (or practically unlimited outside of production)
  10. windowMs: isProduction ? (EXPIRES_IN_AS_SECONDS * 1000) : 1, // Non-Redis configuration in `ms`. Used as a fallback when Redis is not working or active.
  11. // limit each IP to X requests per windowMs
  12. max: 250,
  13. // Don't rate limit requests for 200s and redirects
  14. // Or anything with a status code less than 400
  15. skipSuccessfulRequests: true,
  16. // When available, use Redis; if not, defaults to an in-memory store
  17. store: REDIS_URL && new RedisStore({
  18. client: createRedisClient({
  19. url: REDIS_URL,
  20. db: rateLimitDatabaseNumber,
  21. name: 'rate-limit'
  22. }),
  23. // 1 minute (or practically unlimited outside of production)
  24. expiry: isProduction ? EXPIRES_IN_AS_SECONDS : 1, // Redis configuration in `s`
  25. // If Redis is not connected, let the request succeed as failover
  26. passIfNotConnected: true
  27. })
  28. })
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...