-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
38 lines (29 loc) · 851 Bytes
/
Copy pathserver.js
File metadata and controls
38 lines (29 loc) · 851 Bytes
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
import express from "express";
import cors from "cors";
import dotenv from "dotenv";
import connectDB from "./config/db.js";
import authRoutes from "./routes/authRoutes.js";
import postRoutes from "./routes/postRoutes.js";
dotenv.config();
const app = express();
// CORS Configuration - ADD THIS
app.use(cors({
origin: 'http://localhost:3000', // Your React app
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization']
}));
// Middleware
app.use(express.json());
// Connect DB
connectDB();
// Routes
app.use("/api/auth", authRoutes);
app.use("/api/posts", postRoutes);
// Test route
app.get("/", (req, res) => {
res.send("API is running...");
});
// Start server
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`🚀 Server running on port ${PORT}`));