Spaces:
Sleeping
Sleeping
File size: 1,140 Bytes
c0fb352 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | // 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}`);
}); |