import express, { type Request, type Response, type NextFunction, } from 'express' import cors from 'cors' import dotenv from 'dotenv' import path from 'node:path' import authRoutes from './routes/auth.js' dotenv.config() const app: express.Application = express() const clientDistPath = path.resolve(process.cwd(), 'dist') app.use(cors()) app.use(express.json({ limit: '10mb' })) app.use(express.urlencoded({ extended: true, limit: '10mb' })) /** * API Routes */ app.use('/api/auth', authRoutes) /** * health */ app.use( '/api/health', (_req: Request, res: Response, _next: NextFunction): void => { res.status(200).json({ ok: true, timestamp: new Date().toISOString(), }) }, ) if (process.env.NODE_ENV === 'production') { app.use(express.static(clientDistPath)) app.get('*', (req: Request, res: Response, next: NextFunction) => { if (req.path.startsWith('/api/')) { next() return } res.sendFile(path.join(clientDistPath, 'index.html')) }) } /** * error handler middleware */ app.use((error: Error, _req: Request, res: Response, _next: NextFunction) => { console.error(error) res.status(500).json({ success: false, error: 'Server internal error', }) }) /** * 404 handler */ app.use((_req: Request, res: Response) => { res.status(404).json({ success: false, error: 'API not found', }) }) export default app