-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
104 lines (85 loc) · 2.89 KB
/
Copy pathindex.js
File metadata and controls
104 lines (85 loc) · 2.89 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
101
102
103
104
// Diagnostic server with full request logging
import express from "express";
import cors from "cors";
import dotenv from "dotenv";
console.log('[ROOT] Starting root entry point');
dotenv.config();
const app = express();
// Log all requests
app.use((req, res, next) => {
console.log(`[REQ] → ${req.method} ${req.url} at ${new Date().toISOString()}`);
res.on('finish', () => {
console.log(`[REQ] ← ${req.method} ${req.url} = ${res.statusCode}`);
});
next();
});
app.use(cors({
origin: ['http://localhost:3001', 'http://localhost:3000', 'https://food-review-app-rho.vercel.app'],
credentials: true
}));
app.use(express.json());
// Health check
app.get("/", (req, res) => {
console.log('[HANDLER] Root handler executing');
try {
res.json({ message: "Backend is running!" });
console.log('[HANDLER] Root response sent');
} catch (err) {
console.error('[HANDLER] Root error:', err);
res.status(500).json({ error: err.message });
}
});
app.get("/health", (req, res) => {
console.log('[HANDLER] Health handler executing');
res.json({ ok: true });
});
// Routes from server
console.log('[ROOT] Importing routes...');
try {
const { default: authRoutes } = await import('./server/routes/auth-router.js');
const { default: restaurantsRouter } = await import('./server/routes/restaurants-router.js');
const { default: reviewsRouter } = await import('./server/routes/reviews-router.js');
console.log('[ROOT] Routes imported, mounting...');
app.use('/api/auth', authRoutes);
app.use('/api/restaurants', restaurantsRouter);
app.use('/api/reviews', reviewsRouter);
console.log('[ROOT] Routes mounted');
} catch (err) {
console.error('[ROOT] Error importing routes:', err.message);
console.error('[ROOT] Stack:', err.stack);
}
// 404
app.use((req, res) => {
console.log('[404] No route matched');
res.status(404).json({ error: "Not found" });
});
// Error handler
app.use((err, req, res, next) => {
console.error('[ERROR] Caught error:', err.message);
console.error('[ERROR] Stack:', err.stack);
res.status(500).json({ error: err.message });
});
const PORT = process.env.PORT || 5000;
console.log(`[ROOT] Creating server on port ${PORT}...`);
try {
const server = app.listen(PORT, '0.0.0.0', () => {
console.log(`[ROOT] ✅ Server LISTENING on 0.0.0.0:${PORT}`);
console.log(`[ROOT] Ready to accept requests`);
});
server.on('error', (err) => {
console.error('[ROOT] Server error:', err);
});
server.on('clientError', (err, socket) => {
console.error('[ROOT] Client error:', err);
});
} catch (err) {
console.error('[ROOT] FAILED TO START:', err.message);
process.exit(1);
}
process.on('uncaughtException', (err) => {
console.error('[ROOT] UNCAUGHT EXCEPTION:', err.message);
console.error('[ROOT] Stack:', err.stack);
});
process.on('unhandledRejection', (reason) => {
console.error('[ROOT] UNHANDLED REJECTION:', reason);
});