|
1
|
import { registerRoutes } from "./routes"; |
|
2
|
import { setupVite, serveStatic, log } from "./vite"; |
|
3
|
import { seedDatabase } from "./seed-data"; |
|
4
|
import { seedGuides } from "./seed-guides"; |
|
5
|
import { setupAuth } from "./auth"; |
|
6
|
|
|
7
|
const app = express(); |
|
8
|
app.use(express.json()); |
|
9
|
app.use(express.urlencoded({ extended: false })); |
|
10
|
app.use("/uploads", express.static("uploads")); |
|
11
|
|
|
12
|
setupAuth(app); |
|
13
|
|
|
14
|
app.use((req, res, next) => { |
|
15
|
const start = Date.now(); |
|
16
|
const path = req.path; |
|
17
|
let capturedJsonResponse: Record<string, any> | undefined = undefined; |
|
18
|
|
|
19
|
const originalResJson = res.json; |
|
20
|
res.json = function (bodyJson, ...args) { |
|
21
|
capturedJsonResponse = bodyJson; |
|
22
|
return originalResJson.apply(res, [bodyJson, ...args]); |
|
23
|
}; |
|
24
|
|
|
25
|
res.on("finish", () => { |
|
26
|
const duration = Date.now() - start; |
|
27
|
if (path.startsWith("/api")) { |
|
28
|
let logLine = `${req.method} ${path} ${res.statusCode} in ${duration}ms`; |
|
29
|
if (capturedJsonResponse) { |
|
30
|
logLine += ` :: ${JSON.stringify(capturedJsonResponse)}`; |
|
31
|
} |
|
32
|
|
|
33
|
if (logLine.length > 80) { |
|
34
|
logLine = logLine.slice(0, 79) + "…"; |
|
35
|
} |
|
36
|
|
|
37
|
log(logLine); |
|
38
|
} |
|
39
|
}); |
|
40
|
|
|
41
|
next(); |
|
42
|
}); |
|
43
|
|
|
44
|
(async () => { |
|
45
|
const server = await registerRoutes(app); |
|
46
|
|
|
47
|
// Seed database in background after server starts (non-blocking) |
|
48
|
setImmediate(async () => { |
|
49
|
try { |
|
50
|
await seedDatabase(); |
|
51
|
await seedGuides(); |
|
52
|
} catch (error) { |
|
53
|
console.error("Error seeding database:", error); |
|
54
|
} |
|
55
|
}); |
|
56
|
|
|
57
|
app.use((err: any, _req: Request, res: Response, _next: NextFunction) => { |
|
58
|
const status = err.status || err.statusCode || 500; |
|
59
|
const message = err.message || "Internal Server Error"; |
|
60
|
|
|
61
|
res.status(status).json({ message }); |
|
62
|
throw err; |
|
63
|
}); |
|
64
|
|
|
65
|
// importantly only setup vite in development and after |
|
66
|
// setting up all the other routes so the catch-all route |
|
67
|
// doesn't interfere with the other routes |
|
68
|
if (app.get("env") === "development") { |
|
69
|
await setupVite(app, server); |
|
70
|
} else { |
|
71
|
serveStatic(app); |
|
72
|
} |
|
73
|
|
|
74
|
// ALWAYS serve the app on the port specified in the environment variable PORT |
|
75
|
// Other ports are firewalled. Default to 5000 if not specified. |
|
76
|
// this serves both the API and the client. |
|
77
|
// It is the only port that is not firewalled. |
|
78
|
const port = parseInt(process.env.PORT || '5000', 10); |
|
79
|
server.listen({ |
|
80
|
port, |
|
81
|
host: '0.0.0.0', // Important for deployment - allows external connections |
|
82
|
reusePort: true, |
|
83
|
}, () => { |
|
84
|
log(`serving on port ${port}`); |
|
85
|
}); |
|
86
|
})(); |