Spaces:
Sleeping
Sleeping
| // server.js | |
| import express from 'express'; | |
| import dotenv from 'dotenv'; | |
| import cors from 'cors'; | |
| import { connectDB } from './config/db.js'; | |
| import productRoutes from './routes/productRoutes.js'; | |
| import subcategoryRoutes from './routes/subcategoryRoutes.js'; | |
| // Load environment variables | |
| dotenv.config(); | |
| // Initialize Express App | |
| const app = express(); | |
| // Connect to MongoDB | |
| connectDB(); | |
| // Middleware | |
| // CORS allows your React app (running on port 5173) to send data to this server | |
| app.use(cors({ | |
| origin: [ | |
| 'http://localhost:5173', | |
| 'https://houseofruqa.pages.dev', | |
| 'https://houseofruqa.shop' | |
| ], | |
| credentials: true | |
| })); | |
| // Allows Express to parse JSON data sent from the frontend | |
| app.use(express.json()); | |
| app.use(express.urlencoded({ extended: true })); | |
| // API Routes | |
| app.use('/api/products', productRoutes); | |
| app.use('/api/subcategories', subcategoryRoutes); | |
| // Basic Health Check Route | |
| app.get('/', (req, res) => { | |
| res.send('House of Ruqa API is running...'); | |
| }); | |
| // Start the server | |
| const PORT = process.env.PORT || 5000; | |
| app.listen(PORT, () => { | |
| console.log(`🚀 Server running on port ${PORT}`); | |
| }); |