forked from exponentsoftware/event-managment-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
77 lines (67 loc) · 2.15 KB
/
app.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
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
68
69
70
71
72
73
74
75
76
77
const express = require("express");
const app = express();
const cors = require("cors");
const chalk = require("chalk");
const { default: mongoose } = require("mongoose");
require("dotenv").config();
// Mongo DB Connections
mongoose
.connect(process.env.MONGO_DB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then((response) => {
console.log("MongoDB Connection Succeeded.");
})
.catch((error) => {
console.log("Error in DB connection: " + error);
});
// Middleware Connections
app.use(cors());
app.use(express.json({ limit: "50mb" }));
app.use(express.urlencoded({ limit: "50mb", extended: true }));
app.use((req, res, next) => {
const startTime = new Date();
res.on("finish", () => {
const endTime = new Date();
const responseTime = endTime - startTime;
// Set the default color for the log
let logColor = "white";
// Check the status code and assign the corresponding color
if (res.statusCode === 200) {
logColor = "green";
} else if (res.statusCode >= 400 && res.statusCode <= 599) {
logColor = "red";
}else if (res.statusCode >= 300 && res.statusCode <= 399){
logColor = "yellow"
}
// Log the colored API call details, response time, URL, and status code
console.log(
chalk[logColor](
`API Call - URL: ${req.originalUrl}, Method: ${req.method}, Response Time: ${responseTime}ms, Status Code: ${res.statusCode}`
)
);
});
next();
});
// Error handler
app.use((err, req, res, next) => {
// Handle the error
console.error(err);
// Set the appropriate status code and error response
const statusCode = err.statusCode || 500;
const errorMessage = err.message || "Internal Server Error";
res.status(statusCode).json({ error: errorMessage });
});
// Routes
const eventRouter = require("./routes/event.routes");
const categoryRouter = require("./routes/category");
const userRouter = require("./routes/user");
app.use("/api", eventRouter);
app.use("/api", categoryRouter);
app.use("/api", userRouter);
// Connection
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log("App running in port: " + PORT);
});