javascript - nodejs統(tǒng)計(jì)對(duì)應(yīng)ip地址的對(duì)某個(gè)接口的請(qǐng)求次數(shù)
問(wèn)題描述
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= ' 手機(jī)號(hào)碼: '+phone+' 地址: '+province+city+district+detailaddress+’rn’; let json={'success':true, } fs.writeFile('訂單.txt',data,{flag: ’a’},function(err,result) {if(err) throw err;console.log(’成功’); }) res.json(json);}對(duì)于上面這個(gè)接口,我如何統(tǒng)計(jì)不同ip地址對(duì)其的訪問(wèn)次數(shù)呢?
問(wèn)題解答
回答1:定義一個(gè)全局變量例如 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吧
回答2:var ip = req.headers[’x-forwarded-for’] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress;
拿到ip了,寫(xiě)個(gè)方法計(jì)數(shù)不就好了?
相關(guān)文章:
1. MySQL主鍵沖突時(shí)的更新操作和替換操作在功能上有什么差別(如圖)2. 關(guān)于mysql聯(lián)合查詢一對(duì)多的顯示結(jié)果問(wèn)題3. python中如何計(jì)算t分布的值?4. mysql在限制條件下篩選某列數(shù)據(jù)相同的值5. 數(shù)據(jù)庫(kù) - Mysql的存儲(chǔ)過(guò)程真的是個(gè)坑!求助下面的存儲(chǔ)過(guò)程哪里錯(cuò)啦,實(shí)在是找不到哪里的問(wèn)題了。6. python執(zhí)行cmd命令,怎么讓他執(zhí)行類(lèi)似Ctrl+C效果將其結(jié)束命令?7. python - scrapy url去重8. 實(shí)現(xiàn)bing搜索工具urlAPI提交9. python - Django有哪些成功項(xiàng)目?10. Python從URL中提取域名
