nodejs限制IP一段时间内的访问次数

const Redis = require('ioredis');

const cache = new Redis({
    port: 6300, // Redis port
    host: "192.100.50.256", // Redis host
    password: "123"
});
cache.get('eggsy').then(data => {
    console.log(15, data)
    if (data > 5) {
        throw new Error('big than 5');
    }
    if (!data) {
        cache.set('eggsy', 1, 'EX', 60);//设置60秒内访问次数大于5次,报错
    } else {
        cache.incr('eggsy');
    }

}).catch(err => {
    console.log(err)
})

猜你喜欢

转载自www.cnblogs.com/qiyc/p/11780639.html
今日推荐