-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
53 lines (45 loc) · 1.46 KB
/
server.js
File metadata and controls
53 lines (45 loc) · 1.46 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
require("dotenv").config();
const express = require("express");
const fs = require("fs");
const path = require("path");
const connectToDB = require("./database/db");
const authRoutes = require("./routes/auth-routes");
const homeRoutes = require("./routes/home-routes");
const adminRoutes = require("./routes/admin-routes");
const uploadImageRoutes = require("./routes/image-routes");
const app = express();
const PORT = process.env.PORT || 3000;
// middleware
app.use(express.json());
// serve static files from the "uploads" directory
app.use("/uploads", express.static(path.join(__dirname, "uploads")));
// routes
app.use("/api/auth", authRoutes);
app.use("/api/home", homeRoutes);
app.use("/api/admin", adminRoutes);
app.use("/api/image", uploadImageRoutes);
// test route
app.get("/", (req, res) => {
res.send("🚀 API is running...");
});
const startServer = async () => {
try {
// Create uploads directory if it doesn't exist
const uploadsDir = path.join(__dirname, "uploads");
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir);
}
await connectToDB();
app.listen(PORT, () => {
console.log(`✅ Server is now listening on PORT ${PORT}`);
console.log(`✅ Minimal server listening at http://localhost:${PORT}`);
});
} catch (error) {
console.error(
"❌ Failed to connect to the database. Server is not starting.",
error
);
process.exit(1); // Exit the process with an error code
}
};
startServer();