search

Home  >  Q&A  >  body text

javascript - nodejs统计对应ip地址的对某个接口的请求次数

exports.prodform=function (req, res) {
    let phone=req.body.phone;
    let province=req.body.province;
    let city=req.body.city;
    let district=req.body.district;
    let detailaddress=req.body.detailaddress;
    let data= " 手机号码: "+phone+" 地址: "+province+city+district+detailaddress+'\r\n';
    let json={
        "success":true,
    }
    fs.writeFile("订单.txt",data,{flag: 'a'},function(err,result) {
        if(err) throw err;
        console.log('成功');
    })
    res.json(json);
}
对于上面这个接口,我如何统计不同ip地址对其的访问次数呢?
迷茫迷茫2786 days ago337

reply all(2)I'll reply

  • 怪我咯

    怪我咯2017-04-17 16:38:28

    Define a global variable
    For example ipList = {};

    在exports.prodform里面加入下面的代码:
    let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
    
    if(!!ipList[ip]){
        ipList[ip] = ipList[ip]+1;
    }else{
        ipList[ip] = 1;
    }
    

    Or use redis

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 16:38:28

    var ip = req.headers['x-forwarded-for'] || 
         req.connection.remoteAddress || 
         req.socket.remoteAddress ||
         req.connection.socket.remoteAddress;

    Now that I have the IP, why not just write a method to count it?

    reply
    0
  • Cancelreply