npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

express-rate-limit-redis

v0.0.4

Published

An express rate-limiting middleware using redis as its storage

Readme

这个库来自于一道面试题

用 Redis 和 Typescript 实现一个 RateLimit 限制器,可以指定事件、限制时间、限制次数,例如限制 1 分钟内最多 3 次获取短信验证码,或 10 分钟内最多一次重置密码。

可以实现为一个 express 的中间件。

期望的使用方法

一个 express 中间件, 可以挂在不同的 API 上用来限制一定时间内被特定用户调用得次数

Install

npm i express-rate-limit-redis

Server code

const express = require('express');
const app = express();
const RateLimiter = require('express-rate-limit-redis');
const Redis = require('ioredis');
const client = new Redis();

const limiter = RateLimiter({
  client,
  id: 'verify-phone-number',
  max: 3, // limit each IP to 3 requests per windowMs
  windowMs: 60 * 1000, // 1 minute
});

app.use('/verify-phone-number', limiter);

app.get('/verify-phone-number', (req, res) => {
  res.json({
    msg: 'ok',
  });
});

const limiter2 = RateLimiter({
  client,
  id: 'change-password',
  max: 1, // limit each IP to 1 requests per windowMs
  windowMs: 10 * 60 * 1000, // 10 minute
});

app.get('/change-password', limiter2, (req, res) => {
  res.json({
    msg: 'ok',
  });
});

const { PORT = 8080 } = process.env;
app.listen(PORT);
console.log(`server running on http://localhost:${PORT}`);

实现思路

以 IP (也可以换成其他用户传的参数) 做为 redis 上过期时间为 windowMs 的 key. 用户每访问一次这个 key 对应的 value 递增. 如果到达限制次数 max, 拒绝用户访问.

代码

./src/index.js

一个使用实例

./example/server.js

  1. Start example server
# install dependency
npm i
# ts to js
npm run build
# start example server
node example/server.js
  1. 打开 https://localhost:8080/verify-phone-number
  2. 刷新页面 3 次以后会被 rate limitd

使用了 ioredis-mock 方便测试

可以做的更好

TODO

参考资料