-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (30 loc) · 1 KB
/
index.js
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
import express from "express";
import mongoose from "mongoose";
import bodyParser from "body-parser";
import dotenv from "dotenv";
import cors from "cors"; // Import the cors middleware
import StudentRoute from "./routes/StudentRoute.js";
import UserRoute from "./routes/UserRoute.js";
dotenv.config();
const app = express();
const PORT = process.env.PORT || 4000;
const MONGOURL = process.env.MONGO_URL;
// Middleware
// app.use(cors({ origin: 'http://localhost:3000' })); // Enable CORS for your frontend
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.urlencoded({ extended: true }));
// MongoDB connection
mongoose
.connect(MONGOURL)
.then(() => {
console.log("Database connected successfully.");
app.listen(PORT, () => {
console.log(`Server is running on port: ${PORT}`);
});
})
.catch((error) => console.log("Database connection error:", error));
// API Routes
app.use("/students", StudentRoute);
app.use("/", UserRoute);