-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
150 lines (126 loc) · 4.11 KB
/
Copy pathserver.js
File metadata and controls
150 lines (126 loc) · 4.11 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
// server.js
import express from 'express';
import cors from 'cors';
import path from 'path';
import { fileURLToPath } from 'url';
import dotenv from 'dotenv';
import bodyParser from 'body-parser';
import fs from 'fs';
import matter from 'gray-matter';
import nodemailer from 'nodemailer';
import sgMail from '@sendgrid/mail';
dotenv.config();
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const app = express();
const PORT = process.env.PORT || 5555;
// ES modules path fix
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Markdown posts folder
const POSTS_DIR = path.join(__dirname, 'devPostsMd');
console.log('Dev posts folder path:', POSTS_DIR);
console.log('Exists?', fs.existsSync(POSTS_DIR));
console.log('Files:', fs.existsSync(POSTS_DIR) ? fs.readdirSync(POSTS_DIR) : 'No folder');
// Serve frontend static files in production
// Your Vite config builds to 'build' folder
const buildPath = path.join(__dirname, 'build');
if (process.env.NODE_ENV === 'production') {
app.use(express.static(buildPath));
}
// Middleware
app.use(cors());
app.use(bodyParser.json());
// Simple request logger
app.use((req, res, next) => {
console.log(`[${new Date().toISOString()}] ${req.method} ${req.originalUrl}`);
next();
});
// Helper: Load Markdown posts
const loadPosts = () => {
if (!fs.existsSync(POSTS_DIR)) {
console.warn('Dev posts folder not found:', POSTS_DIR);
return [];
}
const files = fs.readdirSync(POSTS_DIR).filter(f => f.endsWith('.md'));
const posts = files.map((file, idx) => {
try {
const filePath = path.join(POSTS_DIR, file);
const contentRaw = fs.readFileSync(filePath, 'utf8');
const { data, content } = matter(contentRaw);
return {
id: idx + 1,
title: data.title || file.replace('.md', ''),
date: data.date || fs.statSync(filePath).mtime.toISOString().split('T')[0],
content,
};
} catch (err) {
console.error('Error loading post:', file, err);
return null;
}
}).filter(Boolean);
posts.sort((a, b) => new Date(b.date) - new Date(a.date));
console.log('Loaded posts:', posts.map(p => p.title));
return posts;
};
// === API ROUTER ===
const apiRouter = express.Router();
apiRouter.get('/devPosts', (req, res) => {
const posts = loadPosts();
if (!posts.length) console.warn('No dev posts found!');
res.json(posts);
});
apiRouter.post('/contact', async (req, res, next) => {
const { name, email, subject, message } = req.body;
if (!name || !email || !subject || !message)
return res.status(400).json({ error: 'All fields are required' });
try {
const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
});
await transporter.sendMail({
from: email,
to: 'me@joshuamost.com',
subject,
text: `Name: ${name}\nEmail: ${email}\n\nMessage:\n${message}`,
});
res.status(200).json({ message: 'Email sent successfully' });
} catch (err) {
next(err);
}
});
apiRouter.post('/newsletter/signup', async (req, res, next) => {
const { email } = req.body;
if (!email) return res.status(400).json({ error: 'Email is required' });
try {
await sgMail.send({
to: email,
from: 'mostjr@masters.edu',
subject: 'Thanks for subscribing!',
text: 'Welcome to the newsletter!',
html: '<strong>Welcome to the newsletter!</strong>',
});
res.status(200).json({ message: 'Thank you for subscribing!' });
} catch (err) {
next(err);
}
});
app.use('/api', apiRouter);
// === SPA fallback ===
if (process.env.NODE_ENV === 'production') {
app.get(/^\/.*$/, (req, res) => {
res.sendFile(path.join(buildPath, 'index.html'));
});
}
// === Error handling middleware ===
app.use((err, req, res, next) => {
console.error(`[${new Date().toISOString()}] Error on ${req.method} ${req.originalUrl}:`, err);
res.status(500).json({ error: 'Internal server error' });
});
// === Start server ===
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));