Socket
Socket
Sign inDemoInstall

rate-limit-redis

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rate-limit-redis

Provides a Redis store for the express-rate-limit RateLimit middleware.


Version published
Weekly downloads
174K
increased by2.11%
Maintainers
1
Weekly downloads
 
Created

What is rate-limit-redis?

The rate-limit-redis npm package is a Redis-backed store for the express-rate-limit middleware. It allows you to implement rate limiting in your Express applications using Redis as the storage backend. This is particularly useful for distributed applications where you need to share rate limit data across multiple instances.

What are rate-limit-redis's main functionalities?

Basic Rate Limiting

This code demonstrates how to set up basic rate limiting in an Express application using Redis as the storage backend. The rate limiter is configured to allow 100 requests per 15 minutes per IP address.

const rateLimit = require('express-rate-limit');
const RedisStore = require('rate-limit-redis');
const redisClient = require('redis').createClient();

const limiter = rateLimit({
  store: new RedisStore({
    client: redisClient
  }),
  max: 100, // limit each IP to 100 requests per windowMs
  windowMs: 15 * 60 * 1000 // 15 minutes
});

const express = require('express');
const app = express();
app.use(limiter);

app.get('/', (req, res) => {
  res.send('Hello, world!');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Custom Redis Client

This example shows how to use a custom Redis client configuration with the rate-limit-redis package. This is useful if you need to connect to a Redis instance with specific connection settings.

const rateLimit = require('express-rate-limit');
const RedisStore = require('rate-limit-redis');
const redis = require('redis');

const customRedisClient = redis.createClient({
  host: 'custom-redis-host',
  port: 6379,
  password: 'your-redis-password'
});

const limiter = rateLimit({
  store: new RedisStore({
    client: customRedisClient
  }),
  max: 50, // limit each IP to 50 requests per windowMs
  windowMs: 10 * 60 * 1000 // 10 minutes
});

const express = require('express');
const app = express();
app.use(limiter);

app.get('/', (req, res) => {
  res.send('Hello, world!');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Dynamic Rate Limiting

This code demonstrates how to implement dynamic rate limiting based on user properties. In this example, premium users are allowed more requests compared to regular users.

const rateLimit = require('express-rate-limit');
const RedisStore = require('rate-limit-redis');
const redisClient = require('redis').createClient();

const dynamicLimiter = rateLimit({
  store: new RedisStore({
    client: redisClient
  }),
  max: (req, res) => {
    if (req.user && req.user.isPremium) {
      return 200; // higher limit for premium users
    }
    return 100; // default limit
  },
  windowMs: 15 * 60 * 1000 // 15 minutes
});

const express = require('express');
const app = express();
app.use(dynamicLimiter);

app.get('/', (req, res) => {
  res.send('Hello, world!');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Other packages similar to rate-limit-redis

FAQs

Package last updated on 18 May 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc