-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
96 lines (84 loc) · 2.66 KB
/
Copy pathserver.js
File metadata and controls
96 lines (84 loc) · 2.66 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
86
87
88
89
90
91
92
93
94
95
96
// server.js
import express from "express";
import cors from "cors";
import dotenv from "dotenv";
import authRoutes from "./routes/authRoutes.js";
import developerRoutes from "./routes/developerRoutes.js";
import projectRoutes from "./routes/projectRoutes.js";
import reviewRoutes from "./routes/reviewRoutes.js";
import messageRoutes from "./routes/messageRoutes.js";
import statsRoutes from "./routes/statRoutes.js"; // note the singular "statRoutes"
import adminRoutes from "./routes/adminRoutes.js";
import { errorHandler } from "./middleware/errorHandler.js";
dotenv.config();
const app = express();
const PORT = process.env.PORT || 5000;
// =====================
// ✅ CORS Configuration
// =====================
const allowedOrigins = [
"https://devhyb-frontend.vercel.app", // your deployed frontend
"http://localhost:3000", // local dev frontend
];
app.use(
cors({
origin: (origin, callback) => {
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
} else {
console.warn("❌ Blocked by CORS:", origin);
callback(new Error("Not allowed by CORS"));
}
},
credentials: true,
})
);
app.options("*", cors()); // preflight requests
// =====================
// Middleware
// =====================
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Request logging
app.use((req, res, next) => {
console.log(`${new Date().toISOString()} - ${req.method} ${req.path}`);
next();
});
// =====================
// Routes
// =====================
app.use("/api/auth", authRoutes);
app.use("/api/developers", developerRoutes);
app.use("/api/projects", projectRoutes);
app.use("/api/reviews", reviewRoutes);
app.use("/api/messages", messageRoutes);
app.use("/api/stats", statsRoutes);
app.use("/api/admin", adminRoutes);
// =====================
// Health Check
// =====================
app.get("/health", (req, res) => {
res.json({
status: "OK",
timestamp: new Date().toISOString(),
uptime: process.uptime(),
});
});
// =====================
// 404 Handler
// =====================
app.use("*", (req, res) => {
res.status(404).json({ error: "Route not found" });
});
// =====================
// Error Handler
// =====================
app.use(errorHandler);
// =====================
// Start Server
// =====================
app.listen(PORT, () => {
console.log(`🚀 DevHub API running on http://localhost:${PORT}`);
console.log(`📚 Environment: ${process.env.NODE_ENV || "development"}`);
console.log(`🌐 Allowed Frontend: https://devhyb-frontend.vercel.app`);
});