-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·100 lines (87 loc) · 3.19 KB
/
server.js
File metadata and controls
executable file
·100 lines (87 loc) · 3.19 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
97
98
99
100
const express = require("express");
const webpack = require("webpack");
const webpackDevMiddleware = require("webpack-dev-middleware");
const webpackHotMiddleware = require("webpack-hot-middleware");
const path = require("path");
const config = require("./webpack.config.js");
const app = express();
const compiler = webpack(config);
// ============================================
// BASIC MIDDLEWARE
// ============================================
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// ============================================
// API ROUTES - MUST BE FIRST
// ============================================
const apiRouter = express.Router();
// Debug middleware to log all API requests
apiRouter.use((req, res, next) => {
console.log(`API Request: ${req.method} ${req.path} | Original URL: ${req.originalUrl}`);
next();
});
apiRouter.get("/test", (req, res) => {
res.json({ message: "API router is working!", path: req.path });
});
// Catch-all for unmatched API routes (for debugging)
apiRouter.use((req, res, next) => {
console.error(`API route not matched: ${req.method} ${req.path} | Original: ${req.originalUrl}`);
res.status(404).json({ error: "API route not found", method: req.method, path: req.path });
});
// Mount API router - CRITICAL: Must be before webpack middleware
app.use("/api", apiRouter);
// ============================================
// WEBPACK MIDDLEWARE - Only for webpack assets
// ============================================
const webpackMiddleware = webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath,
index: false,
writeToDisk: false,
});
// Only apply webpack middleware to webpack assets (bundle.js, etc.)
app.use((req, res, next) => {
// Skip API routes
if (req.path.startsWith("/api")) {
return next();
}
// Only handle webpack assets
if (req.path === "/bundle.js" || req.path.startsWith("/bundle.js") || req.path.includes("webpack")) {
return webpackMiddleware(req, res, next);
}
next();
});
const hotMiddleware = webpackHotMiddleware(compiler);
app.use((req, res, next) => {
if (req.path.startsWith("/api")) {
return next();
}
if (req.path.includes("__webpack") || req.path.includes("webpack")) {
return hotMiddleware(req, res, next);
}
next();
});
// ============================================
// STATIC FILES - Only for non-API routes
// ============================================
app.use((req, res, next) => {
if (req.path.startsWith("/api")) {
return next();
}
express.static(path.join(__dirname, "public"))(req, res, next);
});
// ============================================
// CATCH-ALL: React SPA routing
// ============================================
app.use((req, res) => {
// This should NEVER be hit for API routes
if (req.path.startsWith("/api")) {
console.error("ERROR: API route reached catch-all! Path:", req.path);
return res.status(404).json({ error: "API route not found" });
}
res.sendFile(path.join(__dirname, "public", "index.html"));
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
console.log(`API routes available at http://localhost:${PORT}/api/*`);
});