V
V
Vitaly Pershin2019-03-19 01:07:34
Node.js
Vitaly Pershin, 2019-03-19 01:07:34

How to prevent frequent requests to node.js server?

I am learning node.js. I connected the Express framework, wrote a small application. From the page I make a post request via ajax and receive json back for further processing. Interested in the question of how to make it so that it was impossible to make too frequent requests. On the client side, it's clear, I can put a stub, stop events, hide buttons, etc. But I'm interested in how to control this on the server side. More precisely, I think I found a solution, the express-rate-limit library. She's doing great, but...

Since the request is made via ajax, requests are not processed when the limit is reached, but I can't figure out how to show this on the page without reloading it? That is, is it possible to get some kind of callback upon reaching the limit, send json with an error to the front to make it clear to the user that he will no longer be able to send a request.

This is how express-rate-limit works now:

const RateLimit = require('express-rate-limit');

const app = express();

app.enable('trust proxy');

var apiLimiter = new RateLimit({
    windowMs: 2 * 60 * 1000, // 2 minutes
    max: 3,
});

app.post("/page", urlencodedParser, apiLimiter, function (request, response) {
//.....
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2019-03-19
@Get-Web

So after all, you need to catch errors during ajax requests, the server will give you an exceeded limit.
There is a separate http code 429 for this.
If the response is not http code 200, then the request failed. More on the situation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question