-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
56 lines (46 loc) · 1.53 KB
/
Copy pathserver.ts
File metadata and controls
56 lines (46 loc) · 1.53 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
import express from "express";
import { createServer as createViteServer } from "vite";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const LOG_FILE = path.join(__dirname, "app.log");
async function startServer() {
const app = express();
const PORT = 3000;
app.use(express.json());
// API routes FIRST
app.get("/api/health", (req, res) => {
res.json({ status: "ok" });
});
app.post("/api/logs", (req, res) => {
const { level, message, timestamp, context } = req.body;
const logEntry = `[${timestamp}] [${level}] ${message} ${context ? JSON.stringify(context) : ''}\n`;
fs.appendFile(LOG_FILE, logEntry, (err) => {
if (err) {
console.error("Failed to write to log file:", err);
return res.status(500).json({ error: "Failed to write log" });
}
res.json({ status: "logged" });
});
});
// Vite middleware for development
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
// Serve static files in production
app.use(express.static(path.join(__dirname, "dist")));
// SPA fallback
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "dist", "index.html"));
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on http://localhost:${PORT}`);
});
}
startServer();