chore: add example env file, improve documentation and code comments
This commit is contained in:
@@ -5,21 +5,18 @@ module.exports = (contactController, securityMw) => {
|
||||
const rateLimit = require('express-rate-limit');
|
||||
const { body, validationResult } = require('express-validator');
|
||||
|
||||
// 🛡️ Configure rate limiting to prevent DDoS and spamming
|
||||
// Configure rate limiting to prevent spam
|
||||
const apiLimiter = rateLimit({
|
||||
windowMs: 15 * 60 * 1000, // 15 minutes
|
||||
max: 5,
|
||||
message: "Too many requests from this IP, please try again after 15 minutes."
|
||||
});
|
||||
|
||||
// Define the route for form submissions with all middleware
|
||||
router.post('/submit-form',
|
||||
apiLimiter,
|
||||
// The security middleware is now a separate step,
|
||||
// containing both the honeypot check and hCaptcha verification.
|
||||
securityMw.formSecurityCheck,
|
||||
[
|
||||
// express-validator: sanitation and validation
|
||||
// Sanitize and validate form data
|
||||
body('firstName').trim().escape(),
|
||||
body('lastName').trim().escape(),
|
||||
body('email').isEmail().normalizeEmail(),
|
||||
@@ -27,7 +24,7 @@ module.exports = (contactController, securityMw) => {
|
||||
body('phone').trim(),
|
||||
body('message').trim().escape(),
|
||||
],
|
||||
// Middleware to handle the express-validator results
|
||||
// Handle validation results
|
||||
(req, res, next) => {
|
||||
const errors = validationResult(req);
|
||||
if (!errors.isEmpty()) {
|
||||
@@ -36,10 +33,8 @@ module.exports = (contactController, securityMw) => {
|
||||
}
|
||||
next();
|
||||
},
|
||||
// The controller, which is the final step
|
||||
contactController.submitForm
|
||||
);
|
||||
|
||||
// Return the configured router
|
||||
return router;
|
||||
};
|
||||
|
Reference in New Issue
Block a user