Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions apps/demos/utils/server/csp-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,26 @@ const demoIndexHandler = (request, response) => {
response.send(fileContent);
};

const createRateLimiter = (windowMs = 60000, maxRequests = 200) => {
const hits = new Map();

setInterval(() => hits.clear(), windowMs);

return (req, res, next) => {
const key = req.ip;
const count = (hits.get(key) || 0) + 1;
hits.set(key, count);

if (count > maxRequests) {
res.status(429).send('Too Many Requests');
return;
}
next();
};
};

const app = express();
app.use(createRateLimiter());
app.use(cookieParser());
Comment thread
EugeniyKiyashko marked this conversation as resolved.
app.use(cspMiddleware);

Expand Down
Loading