-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
44 lines (39 loc) · 1.12 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
const dotenv = require('dotenv');
const mongoose = require('mongoose');
dotenv.config({ path: './config.env' });
process.on('uncaughtException', (err) => {
console.log(err);
process.exit(1);
});
const app = require('./app');
if (!process.env.DATABASE) {
throw new Error('DATABASE environment variable is not defined');
}
// console.log(process.env.DATABASE_PASSWORD);
const DB = process.env.DATABASE.replace(
'<PASSWORD>',
process.env.DATABASE_PASSWORD
).replace('<USERNAME>', process.env.DATABASE_USERNAME);
// console.log(DB);
// if you want to use localdata base then replace db with process.env.LOCAL_DATABASE and declare this config file
mongoose
.connect(DB, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then((con) => {
// console.log(con.connections);
console.log('DB connection successful!');
});
// Server setup
const port = process.env.PORT || 3000;
const server = app.listen(port, () => {
console.log(`App running on port ${port}`);
});
//To handle any promise rejection
process.on('unhandledRejection', (err) => {
console.log(err);
server.close(() => {
process.exit(1);
});
});