Skip to content

Commit

Permalink
Added a rateLimiter.
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-sth committed Dec 10, 2023
1 parent 1e0c73c commit 7d4af3b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
36 changes: 17 additions & 19 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"config": "^3.3.9",
"cors": "^2.8.5",
"express": "^4.18.2",
"express-rate-limit": "^7.1.5",
"joi": "^17.11.0",
"lodash": "^4.17.21",
"mongoose": "^8.0.3",
Expand All @@ -17,7 +18,6 @@
"test": "jest",
"start": "ts-node src/server.ts",
"benchmark": "ts-node benchmark/timeDistribution"

},
"devDependencies": {
"@types/config": "^3.3.3",
Expand Down
3 changes: 2 additions & 1 deletion src/routes/isOnline.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import express, { Request, Response } from 'express';
const router = express.Router();

router.get('/', (_req: Request, res: Response) => {
res.status(200);
res.status(200).send('OK');
});

export = router;
2 changes: 2 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { configureCors } from './startup/cors';
import isOnline from './routes/isOnline';
import project from './routes/project';
import connectToDatabase from './startup/db';
import addRateLimiter from './startup/limitRate';

const app: Express = express();

// startup
configureCors(app);
connectToDatabase();
addRateLimiter(app);

app.use(express.json());
app.use('/', isOnline);
Expand Down
12 changes: 12 additions & 0 deletions src/startup/limitRate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Express } from 'express';
import RateLimit from 'express-rate-limit';

const addRateLimiter = async (app: Express) => {
const limiter = RateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // max 100 requests per windowMs
});

app.use(limiter);
};
export default addRateLimiter;

0 comments on commit 7d4af3b

Please sign in to comment.