Add detailed debugging logs to server.js

This commit is contained in:
George committed 2024-12-29 23:41:46 +01:00
1 parent 3ff4c9db65
commit aedaa9976d
1 file changed
+20 -5
+20 -5
View File
@@ -13,6 +13,13 @@ app.use(cors());
app.use(express.json());
app.use(express.static('public'));
// Log environment variables (only for debugging purposes, do not log sensitive data in production)
console.log('Environment Variables:', {
MONGODB_URI: process.env.MONGODB_URI,
EMAIL_USER: process.env.EMAIL_USER,
EMAIL_TO: process.env.EMAIL_TO,
});
// MongoDB connection
console.log('Connecting to MongoDB with URI:', process.env.MONGODB_URI);
@@ -43,6 +50,15 @@ const transporter = nodemailer.createTransport({
},
});
// Test email configuration
transporter.verify((error, success) => {
if (error) {
console.error('Email transporter configuration failed:', error);
} else {
console.log('Email transporter is configured and ready to send emails');
}
});
// API endpoint for form submission
app.post('/api/consultations', async (req, res) => {
try {
@@ -60,11 +76,13 @@ app.post('/api/consultations', async (req, res) => {
}
// Save to MongoDB
console.log('Attempting to save consultation to MongoDB...');
const consultation = new Consultation({ name, email, message });
await consultation.save();
console.log('Consultation saved to MongoDB:', consultation);
// Send email
console.log('Attempting to send email...');
const mailOptions = {
from: process.env.EMAIL_USER,
to: process.env.EMAIL_TO,
@@ -72,8 +90,6 @@ app.post('/api/consultations', async (req, res) => {
text: `Name: ${name}\nEmail: ${email}\nMessage: ${message}`,
};
console.log('Mail options prepared:', mailOptions);
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error('Error sending email:', error);
@@ -82,8 +98,7 @@ app.post('/api/consultations', async (req, res) => {
message: 'Failed to send email',
});
}
console.log('Email sent:', info.response);
console.log('Email sent successfully:', info.response);
res.status(201).json({
success: true,
message: 'Consultation request received successfully',
@@ -101,4 +116,4 @@ app.post('/api/consultations', async (req, res) => {
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
});