-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
61 lines (50 loc) · 1.38 KB
/
server.ts
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
import express from "express";
import cors from "cors";
import dotenv from "dotenv";
import morgan from "morgan";
import colors from "colors";
// Custom imports
import connectDB from "./config/db";
// Routes imports here
import authRoutes from "./routes/auth_routes";
import accessRoutes from "./routes/access_routes";
import entriesRoutes from "./routes/entries_routes";
import scheduleRoutes from "./routes/schedule_routes"
// Enable colors
colors.enable();
// DOTENV
dotenv.config();
// REST OBJECT
const app = express();
// MIDDLEWARE
app.use(cors());
app.use(express.json()); // Body parser
app.use(morgan("dev"));
// ROUTES
app.get("/", (req, res) => {
res.send("API is running...");
});
app.use("/api/v1/auth", authRoutes);
app.use("/api/v1/access", accessRoutes);
app.use("/api/v1/entries", entriesRoutes);
app.use("/api/v1/schedule",scheduleRoutes);
// PORT
const PORT = process.env.PORT || 8000;
// Connect to DB and start server
const startServer = async () => {
try {
await connectDB();
console.log(colors.cyan.underline("MongoDB connected successfully"));
app.listen(PORT, () => {
console.log(
colors.bgWhite.red.bold(
`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`
)
);
});
} catch (error) {
console.error(colors.red.bold("Failed to connect to MongoDB:"), error);
process.exit(1);
}
};
startServer();