Skip to content

Commit c7f252c

Browse files
author
Christian Furr
committed
added rate limiting
1 parent ba5598b commit c7f252c

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

bun.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"dependencies": {
77
"cors": "^2.8.5",
88
"express": "^4.18.2",
9+
"express-rate-limit": "^7.5.0",
910
},
1011
"devDependencies": {
1112
"nodemon": "^3.0.1",
@@ -73,6 +74,8 @@
7374

7475
"express": ["[email protected]", "", { "dependencies": { "accepts": "~1.3.8", "array-flatten": "1.1.1", "body-parser": "1.20.3", "content-disposition": "0.5.4", "content-type": "~1.0.4", "cookie": "0.7.1", "cookie-signature": "1.0.6", "debug": "2.6.9", "depd": "2.0.0", "encodeurl": "~2.0.0", "escape-html": "~1.0.3", "etag": "~1.8.1", "finalhandler": "1.3.1", "fresh": "0.5.2", "http-errors": "2.0.0", "merge-descriptors": "1.0.3", "methods": "~1.1.2", "on-finished": "2.4.1", "parseurl": "~1.3.3", "path-to-regexp": "0.1.12", "proxy-addr": "~2.0.7", "qs": "6.13.0", "range-parser": "~1.2.1", "safe-buffer": "5.2.1", "send": "0.19.0", "serve-static": "1.16.2", "setprototypeof": "1.2.0", "statuses": "2.0.1", "type-is": "~1.6.18", "utils-merge": "1.0.1", "vary": "~1.1.2" } }, "sha512-28HqgMZAmih1Czt9ny7qr6ek2qddF4FclbMzwhCREB6OFfH+rXAnuNCwo1/wFvrtbgsQDb4kSbX9de9lFbrXnA=="],
7576

77+
"express-rate-limit": ["[email protected]", "", { "peerDependencies": { "express": "^4.11 || 5 || ^5.0.0-beta.1" } }, "sha512-eB5zbQh5h+VenMPM3fh+nw1YExi5nMr6HUCR62ELSP11huvxm/Uir1H1QEyTkk5QX6A58pX6NmaTMceKZ0Eodg=="],
78+
7679
"fill-range": ["[email protected]", "", { "dependencies": { "to-regex-range": "^5.0.1" } }, "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg=="],
7780

7881
"finalhandler": ["[email protected]", "", { "dependencies": { "debug": "2.6.9", "encodeurl": "~2.0.0", "escape-html": "~1.0.3", "on-finished": "2.4.1", "parseurl": "~1.3.3", "statuses": "2.0.1", "unpipe": "~1.0.0" } }, "sha512-6BN9trH7bp3qvnrRyzsBz+g3lZxTNZTbVO2EV1CS0WIcDbawYVdYvGflME/9QP0h0pYlCDBCTjYa9nZzMDpyxQ=="],

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
"dev": "nodemon server.js"
99
},
1010
"dependencies": {
11+
"cors": "^2.8.5",
1112
"express": "^4.18.2",
12-
"cors": "^2.8.5"
13+
"express-rate-limit": "^7.5.0"
1314
},
1415
"devDependencies": {
1516
"nodemon": "^3.0.1"

server.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const express = require('express');
22
const cors = require('cors');
33
const fs = require('fs');
44
const path = require('path');
5+
const rateLimit = require('express-rate-limit');
56

67
const app = express();
78
app.use(cors());
@@ -113,6 +114,25 @@ app.get('/books', (req, res) => {
113114
}
114115
});
115116

117+
// Create a limiter for general routes
118+
const generalLimiter = rateLimit({
119+
windowMs: 15 * 60 * 1000, // 15 minutes
120+
max: 100 // limit each IP to 100 requests per windowMs
121+
});
122+
123+
// Create a stricter limiter for specific routes
124+
const strictLimiter = rateLimit({
125+
windowMs: 15 * 60 * 1000, // 15 minutes
126+
max: 50 // limit each IP to 50 requests per windowMs
127+
});
128+
129+
// Apply general rate limiting to all routes
130+
app.use(generalLimiter);
131+
132+
// Apply stricter rate limiting to specific routes
133+
app.use('/random', strictLimiter);
134+
app.use('/daily', strictLimiter);
135+
116136
const PORT = process.env.PORT || 3000;
117137
app.listen(PORT, () => {
118138
console.log(`Server is running on port ${PORT}`);

0 commit comments

Comments
 (0)