-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
38 lines (25 loc) · 1 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
/**
* @Copyright 2024 mae-mahboube89
* @license MIT
*/
"use strict";
// ----- Node modules -----
const app = require('./app'); // Import the Express app from app.js
const mongoose = require('mongoose'); // Import Mongoose for MongoDB connection
require("dotenv").config(); // Load environment variables from the .env file
const port = process.env.PORT; // Define the port number from the environment variables
// Immediately invoked async function to handle MongoDB connection
(async () => {
try {
// Connect to MongoDB using the URL from environment variables
await mongoose.connect(process.env.MONGODB_URL);
console.log("Mongodb connected!");
} catch (error) {
console.error("Failed to connect to MongoDB", error);
process.exit(1); // Exit the process with an error code
}
})();
// Start the server and listen on the specified port
app.listen(port , () => {
console.log(`Server running on port ${port}.`);
});