-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
53 lines (45 loc) · 2.29 KB
/
index.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
const express = require("express"); // Import the Express module
const morgan = require("morgan"); // Import the Morgan module for logging HTTP requests
const appointmentRoutes = require("./routes/appointmentRoutes.js"); // Import the appointmentRoutes module
const mongoose = require("mongoose"); // Import the Mongoose module for MongoDB connection
const authRoutes = require("./routes/authRoutes"); // Import the authentication routes
const cookieParser = require("cookie-parser");
// MongoDB Credentials
require("dotenv").config();
const DB_USERNAME = process.env.DB_USERNAME; // MongoDB username
const DB_USERPASS = process.env.DB_USERPASS; // MongoDB password
const DB_NAME = process.env.DB_NAME; // MongoDB database name
const DB_CLUSTERS = process.env.DB_CLUSTERS; //MongoDB cluster name
// Express app
const app = express(); // Create an Express application
const port = 3000; // Specify the port number for the server
// Connect to MongoDB
const dbURI = `mongodb+srv://${DB_USERNAME}:${DB_USERPASS}@${DB_CLUSTERS}.ghra5em.mongodb.net/${DB_NAME}?retryWrites=true&w=majority`;
mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true }) // Connect to MongoDB using the provided URI and options
.then((results) => {
// Start the server and listen for requests on the specified port
app.listen(port, "localhost", () => {
console.log(`Connected to DB: '${DB_NAME}' | Listening for requests on port: '${port}'`);
});
})
.catch((err) => console.log(err));
// Middleware
app.use(express.urlencoded({ extended: true })); // Parse URL-encoded form data
app.use(morgan("dev")); // Log HTTP requests in the console using Morgan
app.use(express.json()); // Parse JSON request bodies
app.use(cookieParser());
app.use('/', appointmentRoutes); // Use appointmentRoutes for handling routes starting with '/'
app.use(authRoutes); // Use the authentication routes
//Cookies
app.get("/read-cookies", (req, res) => {
const cookies = req.cookies;
res.json(cookies);
});
// Render the "404" view for any unmatched routes (404 Not Found)
app.use((req, res) => {
res.status(404).render("404", { title: "Not Found" });
});
// Render the "500" view for internal server errors (500 Internal Server Error)
app.use((req, res) => {
res.status(500).render("500", { title: "Internal Server Error" });
});