Backend: Don't Rate Limit in Development

This commit is contained in:
ADAMJR 2021-11-04 22:49:26 +00:00
parent ba1234734a
commit b4fa21aee3
3 changed files with 32 additions and 19 deletions

View File

@ -12,6 +12,8 @@ Custom Frontend and Backend that is similar to Discord.
## Setup
These setup instructions are in a primitive state, and may be improved in the future.
1. Clone the repo.
2. Generate SSH keys.
From app folder:
@ -27,6 +29,7 @@ Custom Frontend and Backend that is similar to Discord.
cd ../backend
npm i
```
4. Configure `backend/.env.example` and rename it to `.env`
---

View File

@ -1,27 +1,36 @@
import { NextFunction, Request, Response } from 'express';
import rateLimit from 'express-rate-limit';
import RateLimitStore from 'rate-limit-mongo';
const windowMs = 10 * 60 * 1000;
// additional layer rate limits
export const extraRateLimit = (maxRequests: number) => rateLimit({
max: maxRequests,
message: JSON.stringify({ message: 'You are being rate limited' }),
store: new RateLimitStore({
uri: process.env.MONGO_URI,
collectionName: 'extraRateLimits',
expireTimeMs: windowMs,
}),
windowMs: windowMs / 2,
});
export const extraRateLimit = (maxRequests: number) => (req: Request, res: Response, next: NextFunction) => {
if (process.env.NODE_ENV === 'dev') return next();
return rateLimit({
max: maxRequests,
message: JSON.stringify({ message: 'You are being rate limited' }),
store: new RateLimitStore({
uri: process.env.MONGO_URI,
collectionName: 'extraRateLimits',
expireTimeMs: windowMs,
}),
windowMs: windowMs / 2,
})(req, res, next);
}
// default layer rate limits
export default rateLimit({
max: 5000,
message: JSON.stringify({ message: 'You are being rate limited' }),
store: new RateLimitStore({
uri: process.env.MONGO_URI,
expireTimeMs: windowMs,
}),
windowMs,
});
export default (req: Request, res: Response, next: NextFunction) => {
if (process.env.NODE_ENV === 'dev') return next();
return rateLimit({
max: 5000,
message: JSON.stringify({ message: 'You are being rate limited' }),
store: new RateLimitStore({
uri: process.env.MONGO_URI,
expireTimeMs: windowMs,
}),
windowMs,
})(req, res, next);
}

1
types/dotenv.d.ts vendored
View File

@ -5,6 +5,7 @@ declare global {
EMAIL_ADDRESS: string;
EMAIL_PASSWORD: string;
MONGO_URI: string;
NODE_ENV: 'dev' | 'prod';
PORT: string;
ROOT_ENDPOINT: string;
WEBSITE_URL: string;