index.ts 919 B

123456789101112131415161718192021222324252627282930313233
  1. import express from "express";
  2. import { createServer } from "http";
  3. import path from "path";
  4. import { fileURLToPath } from "url";
  5. const __filename = fileURLToPath(import.meta.url);
  6. const __dirname = path.dirname(__filename);
  7. async function startServer() {
  8. const app = express();
  9. const server = createServer(app);
  10. // Serve static files from dist/public in production
  11. const staticPath =
  12. process.env.NODE_ENV === "production"
  13. ? path.resolve(__dirname, "public")
  14. : path.resolve(__dirname, "..", "dist", "public");
  15. app.use(express.static(staticPath));
  16. // Handle client-side routing - serve index.html for all routes
  17. app.get("*", (_req, res) => {
  18. res.sendFile(path.join(staticPath, "index.html"));
  19. });
  20. const port = process.env.PORT || 3000;
  21. server.listen(port, () => {
  22. console.log(`Server running on http://localhost:${port}/`);
  23. });
  24. }
  25. startServer().catch(console.error);