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地址对其的访问次数呢?


浮云间
浏览 1373回答 1
1回答

湖上湖

定义一个全局变量例如 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吧
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript