-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
55 lines (48 loc) · 1.47 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const dotenv = require('dotenv');
const mongoose = require('mongoose');
const portfinder = require('portfinder');
process.on('uncaughtException', (err) => {
console.log('UNCAUGHT EXCEPTION! 💥 Shutting down...');
console.log(err.name, err.message);
process.exit(1);
});
dotenv.config({ path: './config.env' }); // this will read the env_variables from the file and import it to the node.js env
const app = require('./app');
const DB = process.env.DATABASE.replace(
// This is for read the hosted data base form config.env and replace password
'<PASSWORD>',
process.env.DATABASE_PASSWORD,
);
mongoose
.connect(DB, {
// this is for some property he will teach it us later on
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
})
.then(() => console.log('DB conections is successfully'));
// server
// const port = 8084;
// app.listen(port, () => {
// console.log(`App is running on port ${port}`);
// });
let server;
portfinder
.getPortPromise()
.then((port) => {
// Start the server on the dynamically assigned port
server = app.listen(port, () => {
console.log(`App is running on port ${port}`);
});
})
.catch((err) => {
console.error('Error finding an available port:', err);
});
process.on('unhandledRejection', (err) => {
console.log('UNCAUGHT EXCEPTION! 💥 Shutting down...');
console.log(err.name, err.message);
server.close(() => {
process.exit(1);
});
});