deafmissoula-website/server/src/index.ts
2024-10-14 14:15:30 -05:00

41 lines
No EOL
1.2 KiB
TypeScript

import express from 'express';
import path from 'path';
import fs from 'fs';
import cors from 'cors';
import bodyParser from 'body-parser';
import sendEmail from './api/sendEmail';
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, '../../client/build')));
app.post('/api/sendEmail', sendEmail);
app.get('/api/calendar-events', (req, res) => {
try {
const calendarDataPath = path.join(__dirname, '../calendarData.json');
if (fs.existsSync(calendarDataPath)) {
const calendarData = fs.readFileSync(calendarDataPath, 'utf-8');
const events = JSON.parse(calendarData);
res.json(events);
} else {
// If file doesn't exist, return an empty array
res.json([]);
}
} catch (error: unknown) {
console.error('Error reading calendar events:', error);
res.status(500).json({ error: 'Failed to fetch calendar events' });
}
});
// Serve React app for all other routes
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../../client/build/index.html'));
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});