-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
75 lines (64 loc) · 2.06 KB
/
Copy pathserver.js
File metadata and controls
75 lines (64 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const express = require('express');
const nodemailer = require('nodemailer');
const cors = require('cors');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 3001;
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static('.')); // Serve static files from current directory
// Email transporter configuration
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_USER || 'your-email@gmail.com',
pass: process.env.EMAIL_PASS || 'your-app-password'
}
});
// Contact form endpoint
app.post('/api/contact', async (req, res) => {
try {
const { name, email, message } = req.body;
// Validate required fields
if (!name || !email || !message) {
return res.status(400).json({
success: false,
error: 'All fields are required'
});
}
// Email options
const mailOptions = {
from: `"${name}" <${email}>`,
to: 'trevormoyinquiries@gmail.com',
subject: `New Contact Form Message from ${name}`,
html: `
<h3>New Contact Form Submission</h3>
<p><strong>Name:</strong> ${name}</p>
<p><strong>Email:</strong> ${email}</p>
<p><strong>Message:</strong></p>
<p>${message.replace(/\n/g, '<br>')}</p>
`,
replyTo: email
};
// Send email
await transporter.sendMail(mailOptions);
res.json({
success: true,
message: 'Email sent successfully!'
});
} catch (error) {
console.error('Error sending email:', error);
res.status(500).json({
success: false,
error: 'Failed to send email. Please try again.'
});
}
});
// Health check endpoint
app.get('/api/health', (req, res) => {
res.json({ status: 'Server is running!' });
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});