gammanexus/server/static.ts

24 lines
707 B
TypeScript

import express from 'express';
import type { Express } from 'express';
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
export function serveStatic(app: Express) {
const distPath = path.resolve(__dirname, "..", "dist", "public");
if (!fs.existsSync(distPath)) {
throw new Error(
`Could not find the build directory: ${distPath}, make sure to build the client first`,
);
}
app.use(express.static(distPath));
// fall through to index.html if the file doesn't exist
app.use("/{*path}", (_req, res) => {
res.sendFile(path.resolve(distPath, "index.html"));
});
}