-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
46 lines (37 loc) · 1.06 KB
/
server.js
File metadata and controls
46 lines (37 loc) · 1.06 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
import express from "express";
import dotenv from "dotenv";
import cors from "cors";
import connectDB from "./db/db.js";
import authRoutes from "./src/routes/authRoutes.js";
import jobRoutes from "./src/routes/jobRoutes.js";
import { errorHandler } from "./src/middleware/errorHandler.js";
const app = express();
dotenv.config();
// Middleware
app.use(express.json()); // body parser
app.use(express.urlencoded({ extended: true }));
app.use(cors({
origin: process.env.CORS_ORIGIN || "http://localhost:5173",
credentials: true,
}));
// Routes
app.use("/api/auth", authRoutes);
app.use("/api/jobs", jobRoutes);
app.get("/", (req, res) => {
res.send("Hello from the Jobify Backend!");
});
app.use(errorHandler);
const PORT = process.env.PORT || 5000;
// Connect to database first, then start server
const startServer = async () => {
try {
await connectDB();
app.listen(PORT, () => {
console.log(`Server is running on PORT: ${PORT}`);
});
} catch (err) {
console.error("Failed to start server:", err.message);
process.exit(1);
}
};
startServer();