Package Exports
- rate-limit-mongo
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (rate-limit-mongo) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Rate Limit Mongo
MongoDB store for the express-rate-limit middleware.
Install
$ npm install --save rate-limit-mongoUsage
var RateLimit = require('express-rate-limit');
var MongoStore = require('rate-limit-mongo');
var limiter = new RateLimit({
store: new MongoStore({
// see Configuration
}),
max: 100,
windowMs: 15 * 60 * 1000
});
// apply to all requests
app.use(limiter);Configuration
uri: string -- uri for connecting to mongodb,
mongodb://127.0.0.1:27017/test_dbfor example. Required if collection hasn't been set.collectionName: string -- name of collection for storing records. Defaults to
expressRateRecordsuser: string -- username for authentication in mongodb
password: string -- password for authentication in mongodb
authSource: string -- db name against which authenticate use. If not set db name from uri will be taken.
collection: object -- mongodb collection instance. Required if uri hasn't been set.
expireTimeMs: integer -- time period, in milliseconds, after which record will be reseted (deleted). Defaults to
60 * 1000. Note that current implementation uses on mongodb ttl indexes - background task that removes expired documents runs every 60 seconds. As a result, documents may remain in a collection during the period between the expiration of the document and the running of the background task. See mongodb ttl indexes doc for more information.resetExpireDateOnChange: boolean -- indicates whether expireDate should be reseted when changed or not. Defaults to
false.errorHandler: function -- function that will be called if error happened during incr, decrement or resetKey methods. Defaults to
_.noop
Methods
MongoStore class provides public methods (incr, decrement, resetKey)
required by express-rate-limit.
In addition following methods provided:
getClient(callback)- ifcollectionwas not passed to the constructor then that method will pass (as second argument) initiated instace of MongoClient to thecallback, otherwisenullwill be passed. Thus this method provides control over connection initiated by the library to the end user. This method is promisified (whenutil.promisifyis presented (node.js >= 8)).