fix: Resolve TypeError by correcting module dependency injection

This commit fixes a TypeError that occurred because the contactRoutes
module was being initialized before the contactController module.

The fix involves:
- Swapping the initialization order in server.js so the contactController is
  initialized before contactRoutes.
- Modifying contactRoutes.js to accept the initialized contactController
  as a dependency, rather than requiring it on its own.
This commit is contained in:
2025-08-17 17:59:44 -05:00
parent 562d831ddf
commit 0b48a79bec
2 changed files with 47 additions and 41 deletions

View File

@@ -1,42 +1,47 @@
const express = require('express'); // The entire module is now a function that accepts 'contactController' as an argument.
const router = express.Router(); module.exports = (contactController) => {
const rateLimit = require('express-rate-limit'); const express = require('express');
const { body, validationResult } = require('express-validator'); const router = express.Router();
const contactController = require('../controllers/contactController'); const rateLimit = require('express-rate-limit');
const { formSecurityCheck } = require('../middleware/securityMw'); const { body, validationResult } = require('express-validator');
// Remove the old require statement since we are getting the controller as an argument
// const contactController = require('../controllers/contactController');
const { formSecurityCheck } = require('../middleware/securityMw');
// 🛡️ Configure rate limiting to prevent DDoS and spamming // 🛡️ Configure rate limiting to prevent DDoS and spamming
const apiLimiter = rateLimit({ const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes windowMs: 15 * 60 * 1000, // 15 minutes
max: 5, max: 5,
message: "Too many requests from this IP, please try again after 15 minutes." message: "Too many requests from this IP, please try again after 15 minutes."
}); });
// Define the route for form submissions // Define the route for form submissions
router.post('/submit-form', router.post('/submit-form',
apiLimiter, apiLimiter,
[ [
// express-validator: sanitation and validation // express-validator: sanitation and validation
body('firstName').trim().escape(), body('firstName').trim().escape(),
body('lastName').trim().escape(), body('lastName').trim().escape(),
body('email').isEmail().normalizeEmail(), body('email').isEmail().normalizeEmail(),
body('organization').trim().escape(), body('organization').trim().escape(),
body('phone').trim(), body('phone').trim(),
body('message').trim().escape(), body('message').trim().escape(),
], ],
// Middleware to handle the express-validator results // Middleware to handle the express-validator results
(req, res, next) => { (req, res, next) => {
const errors = validationResult(req); const errors = validationResult(req);
if (!errors.isEmpty()) { if (!errors.isEmpty()) {
console.error('Validation failed:', errors.array()); console.error('Validation failed:', errors.array());
return res.status(400).json({ success: false, message: 'Invalid form data.' }); return res.status(400).json({ success: false, message: 'Invalid form data.' });
} }
next(); next();
}, },
// The security middleware // The security middleware
formSecurityCheck, formSecurityCheck,
// The controller, which is the final step // The controller, which is the final step
contactController.submitForm contactController.submitForm
); );
module.exports = router; // Return the configured router
return router;
};

View File

@@ -34,10 +34,11 @@ const transporter = nodemailer.createTransport({
}, },
}); });
// Import contactRoutes and contactController
const contactRoutes = require('./routes/contactRoutes');
const contactController = require('./controllers/contactController')(pool, transporter); const contactController = require('./controllers/contactController')(pool, transporter);
// Import contactRoutes and contactController
const contactRoutes = require('./routes/contactRoutes')(contactController);
// Use contactRoutes to connect the modular router to the main app // Use contactRoutes to connect the modular router to the main app
app.use(contactRoutes); app.use(contactRoutes);