-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript_index.js
More file actions
67 lines (54 loc) · 1.74 KB
/
Copy pathjavascript_index.js
File metadata and controls
67 lines (54 loc) · 1.74 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
export const javaScript_index = `// Load environment variables from .env file
import dotenv from "dotenv";
dotenv.config({ path: "./.env" });
// Core imports
import express from "express";
import cors from "cors";
import cookieParser from "cookie-parser";
// Custom utility to connect to MongoDB
import connectDB from "./db/connectDB.js";
// Initialize express app
const app = express();
// ---------------------------
// Middleware Configuration
// ---------------------------
// Enable CORS to allow requests from frontend (e.g., React app)
app.use(
cors({
origin: process.env.CORS_ORIGIN || "http://localhost:5173",
credentials: true, // Allow sending cookies and auth headers
})
);
// Parse incoming JSON and form data
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Parse cookies from incoming requests
app.use(cookieParser());
// Serve static assets from the "public" directory
app.use(express.static("public"));
// ---------------------------
// Route Definitions
// ---------------------------
// TODO: Add your route handlers here
// Example:
// app.use("/api/users", userRoutes);
// ---------------------------
// Start Server
// ---------------------------
const PORT = process.env.PORT || 1604;
// Connect to database and then start server
// Go to ./src/db/connectDB.js file to establish database connection
connectDB()
.then(() => {
app.listen(PORT, () => {
console.log(\`✅ Server is running on http://localhost:\${PORT}\`);
});
// Handle server-level errors
app.on("error", (err) => {
console.error("❌ Server error:", err);
});
})
.catch((err) => {
console.error("❌ MongoDB connection failed:", err);
});
`