-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.js
39 lines (36 loc) · 861 Bytes
/
day12.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const express=require('express')
const app=express()
class ips{
constructor(ip,count){
this.ip=ip;
this.count=count
}
}
const details=[]
const limit=4
function ratelimiter(req,res,next){
const ip=req.ip
let found=false;
for(let i=0;i<details.length;i++){
if (ip === details[i].ip) {
found = true;
console.log("hello");
if (details[i].count >= limit) {
return res.status(429).send("No more requests allowed from this IP");
}
details[i].count++;
console.log(details[i].count);
break;
}
}
if (!found) {
const count = 1;
const d = new ips(ip, count);
details.push(d);
}
next();
}
app.get("/",ratelimiter,(req,res)=>{
res.send("welcome ")
})
app.listen(3000);