mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-24 13:56:39 +00:00
organized routes and routes and added rate limiter
This commit is contained in:
@@ -10,7 +10,6 @@ export const errorHandlerMiddleware: ErrorRequestHandler = (
|
||||
res: Response<ErrorResponse>,
|
||||
next: NextFunction,
|
||||
) => {
|
||||
logger.error(error);
|
||||
const statusCode = error.statusCode || HttpCode.INTERNAL_SERVER_ERROR;
|
||||
res?.status(statusCode).send({
|
||||
data: null,
|
||||
|
||||
3
server/middlewares/index.ts
Normal file
3
server/middlewares/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from "./notFound";
|
||||
export * from "./rateLimit";
|
||||
export * from "./formatError";
|
||||
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