mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-03 17:26:38 +00:00
organized routes and routes and added rate limiter
This commit is contained in:
20
server/middlewares/rateLimit.ts
Normal file
20
server/middlewares/rateLimit.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { rateLimit } from "express-rate-limit";
|
||||
import createHttpError from "http-errors";
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import logger from "@server/logger";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
|
||||
const limit = 100;
|
||||
const minutes = 1;
|
||||
|
||||
export const rateLimitMiddleware = rateLimit({
|
||||
windowMs: minutes * 60 * 1000,
|
||||
limit,
|
||||
handler: (req: Request, res: Response, next: NextFunction) => {
|
||||
const message = `Rate limit exceeded. You can make ${limit} requests every ${minutes} minute(s).`;
|
||||
logger.warn(`Rate limit exceeded for IP ${req.ip}`);
|
||||
return next(createHttpError(HttpCode.TOO_MANY_REQUESTS, message));
|
||||
},
|
||||
});
|
||||
|
||||
export default rateLimitMiddleware;
|
||||
Reference in New Issue
Block a user