-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
85 lines (72 loc) · 2.61 KB
/
Copy pathserver.js
File metadata and controls
85 lines (72 loc) · 2.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
const express = require('express');
const fs = require('fs').promises;
const path = require('path');
const cors = require('cors');
const app = express();
const port = process.env.PORT || 4000;
// Enhanced CORS configuration
const corsOptions = {
origin: '*',
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization']
};
// Middleware
app.use(cors(corsOptions));
app.options(/(.*)/, cors(corsOptions)); // Enable pre-flight for all routes
app.use(express.json());
app.use(express.static(__dirname, {
setHeaders: (res, path) => {
if (path.endsWith('.json')) {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
}
}
}));
// Routes
// Thoughts routes
app.get('/thoughts.json', async (req, res) => {
try {
// Set CORS headers
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
// Handle preflight requests
if (req.method === 'OPTIONS') {
return res.status(200).end();
}
const data = await fs.readFile(path.join(__dirname, 'thoughts.json'), 'utf8');
const jsonData = JSON.parse(data);
// Log for debugging
console.log('Serving thoughts.json with', jsonData.thoughts ? jsonData.thoughts.length : 0, 'thoughts');
res.json(jsonData);
} catch (error) {
console.error('Error reading thoughts.json:', error);
res.status(500).json({
error: 'Failed to read thoughts data',
details: error.message
});
}
});
app.put('/thoughts.json', async (req, res) => {
try {
const data = JSON.stringify(req.body, null, 2);
await fs.writeFile(path.join(__dirname, 'thoughts.json'), data);
res.status(200).json({ message: 'Thoughts data updated successfully' });
} catch (error) {
console.error('Error writing thoughts.json:', error);
res.status(500).json({ error: 'Failed to update thoughts data' });
}
});
// Serve HTML files
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/admin.html', (req, res) => {
res.sendFile(path.join(__dirname, 'admin.html'));
});
// Start server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});