Using express-rate-limit without reloading the entire page

  Kiến thức lập trình

I’m using express-rate-limit to limit the number of connection attempts.

homeController.js

const express = require('express');
const router = express.Router();

//limiter à 5 tentatives de log sur 5 minutes
const limitter = require("express-rate-limit");
const signInlimitter = limitter({ 
    windowMs: 5*60*1000, 
    limit: 1, 
    handler: (req, res, next) => {
        const date = new Date(req.rateLimit.resetTime);
        req.rateLimit.resetTime = date.toLocaleTimeString();
        res.status(429).send(`Trops de tentatives ! Vous pourez réessayer à ${req.rateLimit.resetTime}.`);
    }
});

const homeRegex = /^/(home)?$/;

const homeView = (req, res) => { res.render("home.ejs", { message: "" }); };

router.route(homeRegex).get(homeView).post(signInlimitter, homeView);

module.exports = router;

index.js

...
app.use('/', require('./controllers/homeController')); 
...

So far, not too complicated.
However, when the limit is exceeded, it refreshes the entire page, and only displays the message.

enter image description here
enter image description here

This doesn’t suit me because I would like to display the message in a textbox or an alert() without recalling everything, in the same idea as with a fetch (or ajax).

Thanks !

I have the ejs packages if that helps.

1

LEAVE A COMMENT