feat(backend): serve built frontend in production
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { existsSync } from 'node:fs';
|
||||
import { resolve } from 'node:path';
|
||||
import express, { type Express, type NextFunction, type Request, type Response } from 'express';
|
||||
import { ZodError } from 'zod';
|
||||
import type { Db } from './db/client.js';
|
||||
@@ -17,6 +19,12 @@ export function createApp(db: Db): Express {
|
||||
app.use('/api/sessions', sessionsRouter(db));
|
||||
app.use('/api/stats', statsRouter(db));
|
||||
|
||||
const frontendDist = resolve(import.meta.dirname, '../../frontend/dist');
|
||||
if (existsSync(frontendDist)) {
|
||||
app.use(express.static(frontendDist));
|
||||
app.get('*', (_req, res) => res.sendFile(resolve(frontendDist, 'index.html')));
|
||||
}
|
||||
|
||||
app.use((err: unknown, _req: Request, res: Response, _next: NextFunction) => {
|
||||
if (err instanceof ZodError) {
|
||||
res.status(400).json({ error: { code: 'VALIDATION_ERROR', message: 'Invalid input', details: err.flatten() } });
|
||||
|
||||
Reference in New Issue
Block a user