TypeError:无法读取未定义的属性“ ip”-express-rate-limit

时间:2018-11-05 11:51:54

标签: javascript node.js express rate-limiting

已更新:因为我知道错误与我的代码无关。

大家好!

我注释了所有与express-rate-limit相关的代码,但是此程序包仍然有错误。

我的代码:

const express = require('express');
const router = express.Router();
// const RateLimit = require('express-rate-limit');
const cors = require('cors')

// const apiLimiter = new RateLimit({
//     windowMs: 1000 * 60 * 5,
//     max: 5,
//     // message : "درخواست شما زیاد بوده لطفا 15 دقیقه دیگر دوباره تلاش کنید"
//     handler: function (req, res, /*next*/) {
//         res.json({
//             data: 'درخواست شما زیاد بوده لطفا 15 دقیقه دیگر دوباره تلاش کنید',
//             status: 'error'
//         })
//     }
// });

let prefixes = ["v1"];
for (let prefix of prefixes) {
    router.use(`/${prefix}/`, require(`./${prefix}/home`));
    router.use(`/${prefix}/auth`, cors(), require(`./${prefix}/auth`));
    router.use(`/${prefix}/admin`, cors(), require(`./${prefix}/admin`));

    // router.use(`/${prefix}/`, cors(), apiLimiter, require(`./${prefix}/home`));
    // router.use(`/${prefix}/auth`, cors(), apiLimiter, require(`./${prefix}/auth`));
    // router.use(`/${prefix}/admin`, cors(), apiLimiter, require(`./${prefix}/admin`));
}

module.exports = router;

错误文本为:

C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\node_modules\express-rate-limit\lib\express-rate-limit.js:16
      return req.ip;
                 ^

TypeError: Cannot read property 'ip' of undefined
    at Object.keyGenerator (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\node_modules\express-rate-limit\lib\express-rate-limit.js:16:18)
    at rateLimit (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\node_modules\express-rate-limit\lib\express-rate-limit.js:54:25)
    at Object.<anonymous> (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\app\routes\index.js:20:39)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Application.setRouters (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\app\index.js:35:17)
    at new Application (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\app\index.js:13:14)
    at Object.<anonymous> (C:\Users\Sayyid Ali Sajjadi D\Desktop\gheymat\server.js:4:1)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
[nodemon] app crashed - waiting for file changes before starting...

请帮帮我!我该怎么办?

2 个答案:

答案 0 :(得分:1)

我认为您应该像这样使用它

router.use(`/${prefix}/`, cors(), apiLimiter, require(`./${prefix}/home`));

答案 1 :(得分:1)

如果检查apiLimiter的类型,则会得到'object'。除非typeof apiLimiter是一个“函数”,否则您不能调用apiLimiter()。

大卫的解决方案应该可以解决问题