Reputation:
Updated: because I understood that error is not related to my code.
Hi all!
I comment all of my code that related to express-rate-limit but this package still has an error.
My code:
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;
The error text is:
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...
Plase help me! What should I do?
Upvotes: 0
Views: 2168
Reputation: 755
If you check typeof apiLimiter, you would get 'object'. You cannot call apiLimiter() unless typeof apiLimiter is a 'function'.
David's solution should do the trick
Upvotes: 1
Reputation: 1889
I think you should use it like this
router.use(`/${prefix}/`, cors(), apiLimiter, require(`./${prefix}/home`));
Upvotes: 2