-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
164 lines (139 loc) · 5.61 KB
/
server.js
File metadata and controls
164 lines (139 loc) · 5.61 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// ===============================
// 📦 Imports & Config
// ===============================
import express from "express";
import cors from "cors";
import path from "path";
import dotenv from "dotenv";
import { fileURLToPath } from "url";
import { Resend } from "resend";
dotenv.config();
const app = express();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// ===============================
// 🌍 Middleware
// ===============================
app.use(cors({
origin: [
// Portfolio site
'https://lcportfolio.org',
'https://www.lcportfolio.org',
'https://api.lcportfolio.org',
// Weather site
'https://weather-forecast-global.onrender.com',
// Local development
'http://localhost:10000',
'http://localhost:3000',
'http://localhost:5500',
'http://127.0.0.1:5500'
],
methods: ['GET', 'POST'],
credentials: true
}));
app.use(express.json());
// ✅ Serve static frontend files
app.use(express.static(path.join(__dirname, "public")));
// ===============================
// 📬 Contact API - MUST BE BEFORE WILDCARD!
// ===============================
const resend = new Resend(process.env.RESEND_API_KEY);
app.post("/api/contact", async (req, res) => {
console.log("📬 Contact form received:", req.body);
const { name, email, message } = req.body;
// Validate input
if (!name || !email || !message) {
return res.status(400).json({
success: false,
error: "All fields are required"
});
}
try {
const data = await resend.emails.send({
from: "Portfolio Contact <onboarding@resend.dev>",
to: "chunglonghoa@gmail.com",
subject: `New message from ${name}`,
html: `
<h2>New Message from Portfolio</h2>
<p><strong>Name:</strong> ${name}</p>
<p><strong>Email:</strong> ${email}</p>
<p><strong>Message:</strong></p>
<p>${message}</p>
`,
});
console.log("✅ Email sent successfully:", data);
res.json({ success: true, data });
} catch (error) {
console.error("❌ Email send failed:", error);
res.status(500).json({ success: false, error: error.message });
}
});
app.get("/health", (req, res) => {
res.status(200).json({ status: "ok" });
});
// ===============================
// 🏙️ City Request API - Weather Site
// ===============================
app.post("/api/city-request", async (req, res) => {
console.log("🏙️ City request received:", req.body);
const { cityName, userEmail, message } = req.body;
if (!cityName) {
return res.status(400).json({
success: false,
error: "City name is required"
});
}
try {
const data = await resend.emails.send({
from: "Weather Site <onboarding@resend.dev>",
to: "chunglonghoa@gmail.com",
subject: `🏙️ New City Request: ${cityName}`,
html: `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<h2 style="color: #4a90d9; border-bottom: 2px solid #4a90d9; padding-bottom: 10px;">
🌍 New City Background Request
</h2>
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white; padding: 20px; border-radius: 10px; margin: 20px 0;">
<p style="margin: 0; font-size: 0.9rem; opacity: 0.9;">City Requested:</p>
<p style="margin: 5px 0 0 0; font-size: 1.5rem; font-weight: bold;">${cityName}</p>
</div>
<div style="background: #f8f9fa; padding: 20px; border-radius: 10px; margin: 20px 0;">
<p style="margin: 0 0 10px 0;"><strong>📧 User Email:</strong></p>
<p style="margin: 0; color: #555;">${userEmail || 'Not provided'}</p>
</div>
<div style="background: #f8f9fa; padding: 20px; border-radius: 10px; margin: 20px 0;">
<p style="margin: 0 0 10px 0;"><strong>💬 Message:</strong></p>
<p style="margin: 0; color: #555;">${message || 'No additional details'}</p>
</div>
<hr style="border: none; border-top: 1px solid #eee; margin: 30px 0;">
<p style="color: #888; font-size: 0.85rem; text-align: center;">
Sent from Global Weather Explorer Contact Form
</p>
</div>
`,
});
console.log("✅ City request email sent:", data);
res.json({ success: true, message: "Request sent!" });
} catch (error) {
console.error("❌ City request email failed:", error);
res.status(500).json({ success: false, error: error.message });
}
});
if (!process.env.RESEND_API_KEY) {
console.error("❌ Missing RESEND_API_KEY");
process.exit(1);
}
// ===============================
// 🏠 SPA Fallback - MUST BE LAST!
// ===============================
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
// ===============================
// 🚀 Start Server
// ===============================
const PORT = process.env.PORT || 10000;
app.listen(PORT, () => {
console.log(`🚀 Server running on port ${PORT}`);
});