import express from 'express'; import type { Express } from 'express'; import fs from "node:fs"; import path from "node:path"; export function serveStatic(app: Express) { // In production CJS bundle, we check cwd first (PM2 working directory) const cwdDistPath = path.resolve(process.cwd(), "dist", "public"); if (fs.existsSync(cwdDistPath)) { app.use(express.static(cwdDistPath)); app.use("/{*path}", (_req, res) => { res.sendFile(path.resolve(cwdDistPath, "index.html")); }); return; } // Fallback: check relative to this file const bundleDistPath = path.resolve(__dirname, "..", "public"); if (fs.existsSync(bundleDistPath)) { app.use(express.static(bundleDistPath)); app.use("/{*path}", (_req, res) => { res.sendFile(path.resolve(bundleDistPath, "index.html")); }); return; } throw new Error( `Could not find the build directory. Checked: ${cwdDistPath}, ${bundleDistPath}`, ); }