-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
53 lines (45 loc) · 1.28 KB
/
server.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
// Library Imports
const express = require("express");
const cookies = require("cookie-parser");
const app = express();
const cors = require("cors");
// User Imports
const connectDb = require("./config/dbConnect");
const userRoutes = require("./routes/userRoute");
const errorHandler = require("./middlewares/errorHandler");
// Import environment variables
require("dotenv").config();
const port = process.env.PORT || 3000;
const startServer = async () => {
if (await connectDb()) {
try {
// Start the Express server and listen on the defined port
app.listen(port, () => {
console.log(
`\nSuccessfully Connected to Database...\nListening to Requests at Port: ${port}\nServer Started...`
);
});
} catch (err) {
console.log(err);
}
} else {
console.log("Server Error");
}
};
// Start Server
startServer();
// Configuring middleware
app.use(cors());
app.use(express.json());
app.use(cookies());
app.use((req, res, next) => {
console.log("\nNew Request Made :");
console.log("Host : ", req.hostname);
console.log("Path : ", req.path);
console.log("Method : ", req.method);
next();
});
// Using defined routes for handling various API endpoints
app.use(userRoutes);
// Attach error handling middleware
app.use(errorHandler);