cari

Rumah  >  Soal Jawab  >  teks badan

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地址对其的访问次数呢?
迷茫迷茫2785 hari yang lalu335

membalas semua(2)saya akan balas

  • 怪我咯

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

    定义一个全局变量
    例如 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;
    }
    

    或者使用redis吧

    balas
    0
  • 怪我咯

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

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

    拿到ip了,写个方法计数不就好了?

    balas
    0
  • Batalbalas