-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
48 lines (39 loc) · 2.56 KB
/
server.js
File metadata and controls
48 lines (39 loc) · 2.56 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
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const path = require("path");
const connectDB = require("./config/db");
const app = express();
// ── Middleware ─────────────────────────────────────────────────────────────────
app.use(cors());
app.use(express.json());
// ── API Routes ─────────────────────────────────────────────────────────────────
app.use("/api/cds", require("./routes/cds"));
app.use("/api/markets", require("./routes/markets"));
app.use("/api/trades", require("./routes/trades"));
app.use("/api/users", require("./routes/users"));
// ── Health check ───────────────────────────────────────────────────────────────
app.get("/api/health", (req, res) =>
res.json({ status: "ok", timestamp: new Date().toISOString() })
);
// ── Serve built frontend in production ─────────────────────────────────────────
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "public")));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
}
// ── 404 (API only) ─────────────────────────────────────────────────────────────
app.use("/api/*", (req, res) => res.status(404).json({ error: "Route not found" }));
// ── Error handler ──────────────────────────────────────────────────────────────
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: "Internal server error" });
});
// ── Start ──────────────────────────────────────────────────────────────────────
const PORT = process.env.PORT || 5001;
connectDB().then(() => {
app.listen(PORT, () =>
console.log(`Wakeshi running on port ${PORT} [${process.env.NODE_ENV || "development"}]`)
);
});