Dallas62/express-limit

Feature request: be able to apply same limit for all urls under a specific prefix

Opened this issue · 2 comments

Imagine you have an API where calls to a route need to be limited. But you May have many URLs being under the route you want to limit.

for instance:

You have and API and want to limit all routes starting by /limited. All of them using the same limit.

As far as I know if I setup:

`
const RateLimiter = require('express-limit').RateLimiter;
const InMemoryStore = require('express-limit').InMemoryStore;

const store = new InMemoryStore();

const limit = (options = {}) => {
options.store = store;
return new RateLimiter(options).middleware;
};

app.use('/URI', limit);
router.get('/URI/A', (req, res, next) => {
res.status(200).json({});
});

router.get('/URI/B', (req, res, next) => {
res.status(200).json({});
});
`

both paths: /URI/A & /URI/B will be limited but they won't share the limit. I mean If I setup 1 request per minute 1 can do 1 request per minute to each path not 1 request per minute to ANY of the paths.

It will be great if we could setup a prefix so all routes starting by /URI will share the limit no matter if I access URI/A or URI/B

Seems it will work if we use to create the storage key using req.baseUrl instead the req.url
I wil try to make brach with this modification (based on a param ) an lets see if it gets pulled.
Something like a para like request_field ='url'. allowing it to be 'baseUrl' as well and when creating the index in get middleware use const key = this._prefix + req[this._request_field] + '-' + identifier; instead of const key = this._prefix + req.url + '-' + identifier;

Hi @cafs78

Thanks for your feedbacks !
Since I'm not using this library anymore (moving from nodejs to symfony), I appreciate your implication on this feature !
You can add an options, to allow the first case you describe (all routes share the same counter) or enable a counter per base URI.

I will check your MR ASAP and also update dependencies 😉

Regards