https://github.com/redis-developer/basic-redis-rate-limiting-demo-nodejs
This is a basic Redis Rate Limiting demo written in NodeJS
https://github.com/redis-developer/basic-redis-rate-limiting-demo-nodejs
Last synced: 7 months ago
JSON representation
This is a basic Redis Rate Limiting demo written in NodeJS
- Host: GitHub
- URL: https://github.com/redis-developer/basic-redis-rate-limiting-demo-nodejs
- Owner: redis-developer
- License: mit
- Created: 2020-12-22T01:31:08.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2023-06-27T07:44:48.000Z (over 2 years ago)
- Last Synced: 2025-04-12T03:52:59.354Z (11 months ago)
- Language: JavaScript
- Size: 1.74 MB
- Stars: 11
- Watchers: 5
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Tutorial: Rate Limiting app in NodeJS
## Technical Stack
- Frontend: Next.js
- Backend - NodeJS, ExpressJS, Redis
## How it works
This app has been build using `exoress-rate-limit` and `rate-limit-redis` library which will block connections from a client after surpassing certain amount of requests (default: 10) per time (default: 10 sec)
The application will return after each request the following headers. That will let the user know how many requests they have remaining before the run over the limit.
```
X-RateLimit-Limit: 10
X-RateLimit-Remaining: 9
```
On the 10th run server should return an HTTP status code of **429 Too Many Requests**
### Available commands
```
"PEXPIRE", "MULTI", "DEL", "PTTL", "EXEC", "DECR", "INCR"
```
## Hot to run it locally?
### Prerequisites
- Node - v12.19.0
- NPM - v6.14.8
- Docker - v19.03.13 (optional)
### Local installation
```
git clone https://github.com/redis-developer/basic-redis-rate-limiting-demo-nodejs/
# copy file and set proper data inside
cp .env.example .env
# install dependencies
npm install
# run docker compose or install redis manually
docker network create global
docker-compose up -d --build
npm run dev
```
### Env variables:
- PORT - port that server is listening on.
- REDIS_HOST - host for redis instance. Can be with `redis://` or without.
- REDIS_PORT - port for redis instance.
- REDIS_PASSWORD - password for redis. Running redis by docker-compose, there's no password. Running by https://redislabs.com/try-free/ you have to pass this variable.
- REDIS_ENDPOINT_URI - redis port and host encoded in redis uri take precedence over other environment variables. Can be with `redis://` or without.
- COMPOSE_PROJECT_NAME - only for docker-compose to set project name.
## Deployment
To make deploys work, you need to create free account in https://redislabs.com/try-free/ and get Redis' instance informations - REDIS_ENDPOINT_URI and REDIS_PASSWORD. You must pass them as environmental variables (in .env file or by server config, like `Heroku Config Variables`).
### Google Cloud Run
[](https://deploy.cloud.run/?git_repo=https://github.com/redis-developer/basic-redis-rate-limiting-demo-nodejs.git)
### Heroku
[](https://heroku.com/deploy)
### Vercel
[](https://vercel.com/new/git/external?repository-url=https://github.com/redis-developer/basic-redis-rate-limiting-demo-nodejs&env=REDIS_ENDPOINT_URI,REDIS_PASSWORD)