diff --git a/Dockerfile.dev b/Dockerfile.dev new file mode 100644 index 0000000..784467f --- /dev/null +++ b/Dockerfile.dev @@ -0,0 +1,12 @@ +FROM node:18-alpine + +WORKDIR /app + +COPY backend/package*.json ./ +RUN npm install + +COPY backend/ ./ + +EXPOSE 4000 + +CMD ["node", "src/index.js"] diff --git a/backend/src/index.js b/backend/src/index.js index 8668242..8e246d8 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -2,6 +2,7 @@ import express from 'express'; import cors from 'cors'; import dotenv from 'dotenv'; import path from 'path'; +import fs from 'fs'; import { fileURLToPath } from 'url'; import { initDB } from './db/index.js'; import authRoutes from './routes/auth.js'; @@ -37,12 +38,24 @@ app.get('/api/health', (req, res) => { // Serve static frontend files (for production) const frontendPath = path.join(__dirname, '../../frontend/dist'); -app.use(express.static(frontendPath)); +const frontendExists = fs.existsSync(frontendPath); -// Serve index.html for all non-API routes (SPA support) -app.get('*', (req, res) => { - res.sendFile(path.join(frontendPath, 'index.html')); -}); +if (frontendExists) { + app.use(express.static(frontendPath)); + + // Serve index.html for all non-API routes (SPA support) + app.get('*', (req, res) => { + res.sendFile(path.join(frontendPath, 'index.html')); + }); +} else { + console.log('ℹ️ Frontend dist not found - running in API-only mode (dev)'); + app.get('*', (req, res) => { + res.json({ + message: "What's The Point API - Frontend running separately", + frontend: "http://localhost:5173" + }); + }); +} const PORT = process.env.PORT || 4000; diff --git a/docker-compose.yml b/docker-compose.yml index b176fdd..da8ed33 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,8 +16,10 @@ services: timeout: 5s retries: 5 - web: - build: . + backend: + build: + context: . + dockerfile: Dockerfile.dev restart: unless-stopped environment: PORT: 4000 @@ -36,5 +38,20 @@ services: - ./backend:/app - /app/node_modules + frontend: + build: + context: ./frontend + dockerfile: Dockerfile.dev + restart: unless-stopped + environment: + VITE_API_URL: http://localhost:4000/api + depends_on: + - backend + ports: + - "5173:5173" + volumes: + - ./frontend:/app + - /app/node_modules + volumes: db_data: diff --git a/frontend/Dockerfile.dev b/frontend/Dockerfile.dev new file mode 100644 index 0000000..4b1f1ce --- /dev/null +++ b/frontend/Dockerfile.dev @@ -0,0 +1,12 @@ +FROM node:18-alpine + +WORKDIR /app + +COPY package*.json ./ +RUN npm install + +COPY . . + +EXPOSE 5173 + +CMD ["npm", "run", "dev"]