import express, { type Express } from "express"; import fs from "fs"; import path from "path"; export function serveStatic(app: Express) { const distPath = process.env.NODE_ENV === "development" ? path.resolve(import.meta.dirname, "../..", "dist", "public") : path.resolve(import.meta.dirname, "..", "public"); if (!fs.existsSync(distPath)) { console.error( `Could not find the build directory: ${distPath}, make sure to build the client first` ); } app.use(express.static(distPath, { index: false })); app.use("*", (req, res) => { // Missing files under /assets or any path with an extension must return 404. // Falling back to index.html causes browsers to report MIME errors on stale chunks. const requestPath = req.originalUrl.split("?")[0]; if (path.extname(requestPath)) { res.status(404).type("text/plain").send("Not found"); return; } res.sendFile(path.resolve(distPath, "index.html")); }); }