-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
63 lines (55 loc) · 1.56 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
54
55
56
57
58
59
60
61
62
63
/* eslint-disable no-console */
// Importing configuration modules
import '../config.js';
import 'dotenv/config';
// Importing external modules
import mongoose from 'mongoose';
import chalk from 'chalk';
import app from './app.js';
/**
* Server setup script for initializing the Express server and connecting to the MongoDB database.
*
* @function
* @throws {Error} If there is an error during database connection or server start.
*
* @returns {void}
*/
const setupServer = async () => {
try {
/**
* Setting up the server port.
* @type {number}
*/
const PORT = process.env.PORT || 4000;
/**
* Logging developer mode information for the Node environment.
* @type {string}
*/
if (process.env.NODE_ENV === 'development') {
console.log(`${chalk.yellow('[DEBUG]')} Developer mode enabled. Console logging is active.`);
}
/**
* Connecting to the MongoDB database.
* @type {mongoose.Connection}
*/
const dbConnection = await mongoose.connect(process.env.MONGODB_URI, {});
/**
* Starting the Express server and logging success message.
* @type {void}
*/
app.listen(PORT, () => {
console.log(`${chalk.green('[SUCCESS]')} API is running on: http://localhost:${PORT}/api`);
});
// Return the database connection for potential future use
return dbConnection;
} catch (error) {
/**
* Logging errors during database connection or server start.
* @type {Error}
*/
console.error(error);
throw error;
}
};
// Call the setup function
setupServer();