Skip to content

Commit

Permalink
add Rate limiter middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
AUGER Jean-François committed Apr 10, 2024
1 parent 3296010 commit 4d92b4b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
15 changes: 15 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dotenv": "^16.4.5",
"ejs": "^3.1.9",
"express": "^4.19.2",
"express-rate-limit": "^7.2.0",
"express-session": "^1.17.3",
"helmet": "^7.1.0",
"http-errors": "~2.0.0",
Expand Down
20 changes: 20 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@
const express = require('express');
const router = express.Router();
const url = require('url');
const rateLimit = require("express-rate-limit");

// Rate limiter middleware
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 250, // limit each IP/user to 500 requests per windowMs
standardHeaders: 'draft-7', // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
skipSuccessfulRequests: true, // Do not count successful requests
handler: function (req, res, next) {
res.locals.message = "Rate limit exceeded";
res.locals.error = "Too many requests, please try again later.";
res.locals.status = 429;
// render the error page
res.status(res.locals.status);
res.render('error');
},
});

router.use(limiter);

// eslint-disable-next-line no-unused-vars
router.get('/', function (req, res, _next) {
Expand Down

0 comments on commit 4d92b4b

Please sign in to comment.