-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
71 lines (61 loc) · 1.91 KB
/
Copy pathserver.ts
File metadata and controls
71 lines (61 loc) · 1.91 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
import express from 'express';
import path from 'path';
import fs from 'fs/promises';
import { fileURLToPath } from 'url';
import { createServer as createViteServer } from 'vite';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const STORAGE_DIR = path.join(__dirname, 'Storage');
const NOTES_FILE = path.join(STORAGE_DIR, 'notes.json');
async function ensureStorage() {
try {
await fs.access(STORAGE_DIR);
} catch {
await fs.mkdir(STORAGE_DIR, { recursive: true });
// Initialize empty notes file if not exists
await fs.writeFile(NOTES_FILE, JSON.stringify([]));
console.log('Created Storage directory and notes.json');
}
}
async function startServer() {
await ensureStorage();
const app = express();
const PORT = 3000;
app.use(express.json({ limit: '10mb' }));
// API Routes
app.get('/api/notes', async (req, res) => {
try {
const data = await fs.readFile(NOTES_FILE, 'utf-8');
res.json(JSON.parse(data));
} catch (error) {
res.status(500).json({ error: 'Failed to read notes' });
}
});
app.post('/api/notes', async (req, res) => {
try {
const notes = req.body;
await fs.writeFile(NOTES_FILE, JSON.stringify(notes, null, 2));
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: 'Failed to save notes' });
}
});
// Vite Middleware for development
if (process.env.NODE_ENV !== 'production') {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: 'spa',
});
app.use(vite.middlewares);
} else {
// Production static files
const distPath = path.join(__dirname, 'dist');
app.use(express.static(distPath));
app.get('*', (req, res) => {
res.sendFile(path.join(distPath, 'index.html'));
});
}
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server running on http://localhost:${PORT}`);
});
}
startServer();