Skip to content

Commit 8dff7f5

Browse files
authored
Enhance server with optional rate limiting
Added optional rate limiting middleware to the server.
1 parent 96462ae commit 8dff7f5

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

‎index.js‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
constexpress=require("express");
2+
constrateLimit=require("express-rate-limit");
23

34
classStartServer{
4-
staticlisten(port){
5+
staticlisten(port,options={}){
56
constapp=express();
67

7-
// Middleware (optional)
8+
if(options.rateLimit){
9+
constlimiter=rateLimit({
10+
windowMs: options.rateLimit.windowMs||60000,
11+
max: options.rateLimit.max||100
12+
});
13+
app.use(limiter);
14+
}
15+
816
app.use(express.json());
917

10-
// Default route (optional)
1118
app.get("/",(req,res)=>{
1219
res.send("Server is running!");
1320
});
1421

15-
// Start the server and log the default message
1622
app.listen(port,()=>{
1723
console.log(`Server is running on port ${port}`);
1824
});

0 commit comments

Comments
(0)