简体   繁体   English

为什么 express-rate-limit 没有按预期工作?

[英]Why is express-rate-limit not working as expected?

The problem is that the rate limit is not enforced for the amount of time I specify.问题是在我指定的时间内没有强制执行速率限制。 Instead of lasting 35 minutes, it lasts for only about 20 seconds.它没有持续 35 分钟,而是只持续了大约 20 秒。 Also, if I keep making the request, the limit is always enforced, so that seems to refresh the time limit, which I think is also unexpected.另外,如果我一直提出请求,限制总是被强制执行,所以这似乎刷新了时间限制,我认为这也是意想不到的。

Apart from these issues, it works as expected, limiting the number of requests I specify in "max", as long as I make them quickly enough.除了这些问题之外,它按预期工作,限制了我在“最大”中指定的请求数量,只要我足够快地发出请求即可。 I have tested locally, and on a Heroku server.我已经在本地和 Heroku 服务器上进行了测试。

Here is the relevant code:这是相关代码:

app.js应用程序.js

var express = require('express');
var dbRouter = require('./routes/db');

var limiter = require('express-rate-limit');
var app = express();

app.set('trust proxy', 1);

// This is a global limiter, not the one I'm having issues with. 
// I've tried removing it, but the issue remained.
app.use(limiter({
  windowMs: 10000,
  max: 9
}));

app.use('/db', dbRouter);

module.exports = app;

db.js数据库.js

var express = require('express');
var router = express.Router();

var level_controller = require('../controllers/levelController');

var limiter = require('express-rate-limit');

var level_upload_limiter = limiter({
    windowMS: 35 * 60 * 1000,
    max: 1,
    message: 'Too many level uploads. Please try again in about 30 minutes.'
});

router.post('/level/create', level_upload_limiter, level_controller.level_create_post);

module.exports = router;

levelController.js levelController.js

exports.level_create_post = [
    (req, res, next) => {
        
        // ...
        
    }
];

It's the typo you made in your settings: windowMS -> windowMs这是您在设置中输入的错误: windowMS -> windowMs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM