import express from 'express';
import path from 'path';
import fs from 'fs';
import { createServer as createViteServer } from 'vite';
import dotenv from 'dotenv';
dotenv.config();
async function startServer() {
const app = express();
const PORT = 3000;
app.use(express.json());
// API route to send email via SMTP (supporting both standard and PHP endpoint pathways)
app.post(['/api/send-email', '/send-email.php'], async (req, res) => {
const { name, email, phone, company, subject, message, isEstimate, estimateSummary } = req.body;
if (!name || !email) {
return res.status(400).json({ error: 'Missing name or email' });
}
// Generate Email Content
const emailSubject = isEstimate
? `LiteCharms Project Estimate Proposal: ${subject}`
: `LiteCharms General Service Inquiry: ${subject}`;
let breakdownHtml = '';
if (isEstimate && estimateSummary && Array.isArray(estimateSummary.breakdown)) {
breakdownHtml = estimateSummary.breakdown
.map((b: any) => `
${b.item}: ${b.price}`)
.join('');
}
const emailHtml = `
LiteCharms
MIDRAND, SOUTH AFRICA
${isEstimate ? 'New Project Estimate Inquiry' : 'New General Service Inquiry'}
| Client Name: |
${name} |
| Email Address: |
${email} |
| Phone Number: |
${phone || 'Not Provided'} |
| Company: |
${company || 'Not Provided'} |
| Subject: |
${subject} |
${isEstimate && estimateSummary ? `
Selected Scope & Custom Pricing Range
Estimated Range: R ${estimateSummary.totalMin?.toLocaleString('en-ZA')} to R ${estimateSummary.totalMax?.toLocaleString('en-ZA')}
Roadmap: ~${estimateSummary.timelineWeeks} Weeks
${breakdownHtml ? `
` : ''}
` : ''}
Client Message / Brief:
${(message || 'No additional notes provided.').replace(/\n/g, '
')}
This is an automated dispatch from the LiteCharms website server.
`;
try {
const response = await fetch('https://messenger.api.khongisa.co.za/api/send-email', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
htmlBody: emailHtml,
from: 'contact@litecharms.co.za',
to: 'contact@litecharms.co.za',
subject: emailSubject,
}),
});
if (response.ok) {
return res.json({ success: true, message: 'Email sent successfully via Messenger API' });
} else {
const errorText = await response.text();
return res.status(500).json({ error: 'External API Error', details: errorText });
}
} catch (error: any) {
console.error('Server proxy email send error:', error);
return res.status(500).json({ error: 'Server error sending email', details: error.message });
}
});
// Vite middleware setup
if (process.env.NODE_ENV !== 'production') {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: 'spa',
});
app.use(vite.middlewares);
} else {
const distPath = path.join(process.cwd(), 'dist');
app.use(express.static(distPath));
app.get('*', (req, res) => {
res.sendFile(path.join(distPath, 'index.html'));
});
}
app.listen(PORT, '0.0.0.0', () => {
console.log(`LiteCharms full-stack application running on http://localhost:${PORT}`);
});
}
startServer();