-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
69 lines (59 loc) · 1.74 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const dotenv = require('dotenv');
const mongoose = require('mongoose');
const { Log } = require('logs-colorifier');
// UNCAUGHT EXCEPTION(Errors occured in synchronous code, but never handles)
// Should be defined in the top-level
process.on('uncaughtException', (err) => {
Log.error(err.name, err.message);
Log.error('UNCAUGHT EXCEPTION! Shutting down...');
process.exit(1);
});
dotenv.config({ path: './config.env' });
const app = require('./app');
// DATABASE
const DB = process.env.DATABASE.replace(
'<PASSWORD>',
process.env.DATABASE_PASSWORD
);
// Create connection
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
.then(() => Log.success('DB connection successful')); // resolve
// .catch((err) => console.log(err)); // reject
/*
// Creating Document
const testTour = new Tour({
name: 'The Park Camper',
price: 997,
});
testTour
.save()
.then((doc) => console.log(doc))
.catch((err) => console.log('Error 💥: ', err));
*/
// console.log(process.env);
const port = process.env.PORT || 3000;
const server = app.listen(port, () => {
Log.success(`App running on port ${port}...`);
});
// UNHANDLED PROMISE REJEJCTION(Errors occured in asynchronous code, but never handles)
process.on('unhandledRejection', (err) => {
console.log(err.name, err.message);
console.log('UNHANDLED REJECTION! Shutting down...');
server.close(() => {
process.exit(1); // 0--> success, 1--> exceptions
});
});
// console.log(x);
// SIGTERM--> A signal that stops the program from running
process.on('SIGTERM', () => {
console.log('👋zz SIGTERM RECEIVED, shutting down gracefully');
server.close(() => {
console.log('💥 process terminated');
});
});