1- import formidable from "formidable " ;
1+ import cors from "cors " ;
22import express from "express" ;
3+ import rateLimit from "express-rate-limit" ;
4+ import formidable from "formidable" ;
35import fs from "fs" ;
46import http from "http" ;
57import https from "https" ;
6- import cors from "cors" ;
7- import path from "path" ;
88import os from "os" ;
9+ import path from "path" ;
910import { fileURLToPath } from "url" ;
1011
1112const __filename = fileURLToPath ( import . meta. url ) ;
@@ -23,17 +24,15 @@ if (!fs.existsSync(distPath)) {
2324
2425const app = express ( ) ;
2526
26- // Rate limit to 100 req/min per IP (dev-only implementation)
27- const rateLimit = new Map ( ) ;
28- setInterval ( ( ) => rateLimit . clear ( ) , 60000 ) ; // Clear every minute to prevent memory growth
29- app . use ( ( req , res , next ) => {
30- const ip = req . ip ;
31- const count = ( rateLimit . get ( ip ) || 0 ) + 1 ;
32- rateLimit . set ( ip , count ) ;
33- if ( count > 100 ) return res . status ( 429 ) . send ( "Too many requests" ) ;
34- next ( ) ;
27+ // Rate limiting
28+ const limiter = rateLimit ( {
29+ windowMs : 15 * 60 * 1000 , // 15 minutes
30+ max : 100 , // Limit each IP to 100 requests per windowMs
31+ message : "Too many requests from this IP, please try again later." ,
3532} ) ;
3633
34+ app . use ( limiter ) ;
35+
3736app . use (
3837 cors ( {
3938 origin : ( origin , callback ) => {
0 commit comments