-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectDbApproch1.js
More file actions
37 lines (31 loc) · 1.17 KB
/
Copy pathconnectDbApproch1.js
File metadata and controls
37 lines (31 loc) · 1.17 KB
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
import mongoose from "mongoose";
import { config } from "dotenv";
import express from "express";
// import { DB_NAME } from "./constants";
// Load environment variables from the .env file
config();
// Access the MongoDB connection URL from the environment variables
const MONGODB_URL = process.env.MONGODB_URL
// Create an instance of the express application
const app = express();
(async () => {
try {
// Connect to MongoDB using Mongoose
await mongoose.connect(`${MONGODB_URL}`);
// Log a successful connection to the database
console.log("MongoDB is connected to the database");
// Set up error handling for the express app
app.on("error", (error) => {
console.log("Express App Error:", error);
throw error;
});
// Start the express app after successful database connection
app.listen(process.env.PORT || 8080, () => {
console.log(`App is listening on port ${process.env.PORT || 8080}`);
});
} catch (error) {
// Log and throw any errors that occur during the setup
console.error("Setup Error:", error.message);
throw error;
}
})();