-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor server initialization and database connection logic for impr…
…oved clarity and error handling
- Loading branch information
Showing
1 changed file
with
12 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,15 @@ | ||
import { app } from "./app.js"; | ||
import {app} from "./app.js"; | ||
import ConnectDB from "./db/index.js"; | ||
import dotenv from "dotenv"; | ||
import dotenv from 'dotenv' | ||
dotenv.config({ path: './.env' }) | ||
|
||
// Load environment variables | ||
dotenv.config({ path: "./.env" }); | ||
|
||
// Connect to the database | ||
(async () => { | ||
try { | ||
await ConnectDB(); | ||
console.log("Database connected successfully!"); | ||
} catch (error) { | ||
console.error(`Database connection error: ${(error as Error).message}`); | ||
process.exit(1); // Exit if the database connection fails | ||
} | ||
})(); | ||
|
||
// Export the app (this is optional in case you want to reuse it elsewhere) | ||
export { app }; | ||
const PORT = 8080; | ||
ConnectDB().then(() => { | ||
app.listen(PORT, () => { | ||
console.log(`Server is running on port ${PORT}`); | ||
}); | ||
}).catch((error) => { | ||
console.error(`Errors: ${(error as Error).message}`); | ||
process.exit(1); | ||
}); |