Skip to content

Commit 6f43ee1

Browse files
committed
limit
1 parent ba2577d commit 6f43ee1

3 files changed

Lines changed: 34 additions & 11 deletions

File tree

package-lock.json

Lines changed: 21 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"cors": "^2.8.5",
1313
"dotenv": "^16.3.1",
1414
"express": "^4.18.2",
15+
"express-rate-limit": "^7.5.0",
1516
"jsonwebtoken": "^9.0.2",
1617
"mongoose": "^8.0.3"
1718
},
@@ -21,4 +22,4 @@
2122
"keywords": [],
2223
"author": "",
2324
"license": "ISC"
24-
}
25+
}

server.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@ require('dotenv').config();
22
const express = require('express');
33
const mongoose = require('mongoose');
44
const cors = require('cors');
5+
const rateLimit = require('express-rate-limit');
56
const booksRoute = require('./routes/books');
67
const authRoute = require('./routes/auth');
78
const usersRoute = require('./routes/users');
89

910
const app = express();
1011
const port = process.env.PORT || 3000;
1112

12-
app.use(cors());
13+
const limiter = rateLimit({
14+
windowMs: 15 * 60 * 1000,
15+
max: 100,
16+
message: 'You requested too much! PLEASE STOP!',
17+
standardHeaders: true,
18+
legacyHeaders: false,
19+
});
1320

21+
app.use(limiter);
22+
23+
app.use(cors());
1424
app.use(express.json());
1525

1626
const mongoDBUri = process.env.MONGODB_URI;

0 commit comments

Comments
 (0)