Skip to content

Commit

Permalink
✏️ Optimize - Rate Limiting #427 - Add rate limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlee-dev committed Nov 9, 2020
1 parent 45c85f2 commit b803a21
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-rate-limit": "^5.1.3",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.10.13",
"morgan": "^1.10.0",
Expand All @@ -51,6 +52,7 @@
"@types/cookie-parser": "^1.4.2",
"@types/cors": "^2.8.8",
"@types/express": "^4.17.8",
"@types/express-rate-limit": "^5.1.0",
"@types/jsonwebtoken": "^8.5.0",
"@types/mongoose": "^5.7.37",
"@types/morgan": "^1.9.2",
Expand Down
3 changes: 3 additions & 0 deletions src/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import mongoose from "mongoose";
import morgan from "morgan";
import path from "path";

import rateLimiter from "./middleware/rateLimiter";

import { Controller } from "./types";

class App {
Expand Down Expand Up @@ -33,6 +35,7 @@ class App {

this.app.use(cookieParser());
this.app.use(express.json());
this.app.use(rateLimiter);
if (process.env.NODE_ENV !== "test") {
this.app.use(morgan("dev"));
}
Expand Down
10 changes: 10 additions & 0 deletions src/server/middleware/rateLimiter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import rateLimit from "express-rate-limit";

const rateLimiter = rateLimit({
windowMs: 24 * 60 * 60 * 1000, // 24 hrs in milliseconds
max: 100,
message: "You have exceeded the limit of requests.",
headers: true,
});

export default rateLimiter;

0 comments on commit b803a21

Please sign in to comment.