diff --git a/package.json b/package.json index e78f195..fa25814 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "type": "module", "scripts": { "dev": "vite", + "start": "node server/index.js", "build": "tsc && vite build", "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0", "preview": "vite preview", diff --git a/server/index.js b/server/index.js index 9ac9914..f212c10 100644 --- a/server/index.js +++ b/server/index.js @@ -16,11 +16,22 @@ import userRoutes from './routes/users.js'; // Environment setup const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); -dotenv.config({ path: join(__dirname, '..', '.env') }); +dotenv.config({ path: join(__dirname, '..', '.env') }); const app = express(); const PORT = process.env.PORT || 5000; +// Add production configuration +if (process.env.NODE_ENV === 'production') { + // Serve static files from the React app + app.use(express.static(join(__dirname, '../dist'))); + + // Handle React routing, return all requests to React app + app.get('*', (req, res) => { + res.sendFile(join(__dirname, '../dist', 'index.html')); + }); +} + app.use(cors({ origin: process.env.CLIENT_URL, credentials: true, @@ -34,13 +45,11 @@ app.use(express.json()); app.use(helmet()); app.use(morgan('dev')); -// Routes app.use('/api/auth', authRoutes); app.use('/api/subscriptions', subscriptionRoutes); app.use('/api/reminders', reminderRoutes); app.use('/api/users', userRoutes); -// MongoDB connection try { await mongoose.connect(process.env.MONGODB_URI); console.log('Connected to MongoDB successfully'); @@ -49,7 +58,6 @@ try { process.exit(1); } -// Start server app.listen(PORT, () => { console.log(`Server running on port ${PORT}`); }); \ No newline at end of file diff --git a/src/services/api.ts b/src/services/api.ts index 37a80e9..c471fbe 100644 --- a/src/services/api.ts +++ b/src/services/api.ts @@ -1,7 +1,9 @@ import axios from 'axios'; import { AuthResponse, NotificationPreferences, Subscription, SubscriptionInput, User } from '../types'; -const BASE_URL = 'http://localhost:5000/api'; +const BASE_URL = process.env.NODE_ENV === 'production' + ? '/api' // In production, use relative path + : 'http://localhost:5000/api'; // In development, use local server const api = axios.create({ baseURL: BASE_URL, diff --git a/staticwebapp.config.json b/staticwebapp.config.json new file mode 100644 index 0000000..d2c065b --- /dev/null +++ b/staticwebapp.config.json @@ -0,0 +1,22 @@ +{ + "navigationFallback": { + "rewrite": "/index.html", + "exclude": ["/images/*", "/css/*", "/js/*", "/api/*"] + }, + "routes": [ + { + "route": "/api/*", + "allowedRoles": ["anonymous"] + }, + { + "route": "/*", + "serve": "/index.html", + "statusCode": 200 + } + ], + "globalHeaders": { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", + "Access-Control-Allow-Headers": "Content-Type, Authorization" + } +} \ No newline at end of file diff --git a/vite.config.ts b/vite.config.ts index bc400a6..ac6fa68 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -7,6 +7,7 @@ export default defineConfig({ global: 'globalThis', 'process.env': process.env }, + base: '/', resolve: { alias: { 'stream': 'stream-browserify',