-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
40 lines (33 loc) · 1.09 KB
/
app.js
File metadata and controls
40 lines (33 loc) · 1.09 KB
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
'use strict';
import config from './src/config/config.js';
import app from './src/express.js';
import mongoose from 'mongoose';
const port = config.port || 3000;
// Console log will output which DB type it is
// connected to based on development or production
// environment and hide the MongoDB Atlas
// connection string so username/password isn't exposed
let dbUri = "MongoDB Atlas" // production environment is connected to Atlas
if (process.env.NODE_ENV === 'development') {
dbUri = 'Local MongoDB Connection'
}
//setup db
mongoose
.connect(config.mongoUri)
.then(console.log(`Connecting to DonationDB via ${dbUri}`))
.catch((error) => console.error(error));
const db = mongoose.connection;
// triggered on first request (one time event listener)
db.once('open', function () {
console.log('Connected to DonationDB');
});
//handles errors AFTER initial connection
db.on('error', err => {
console.error(`trouble connecting to the db`);
})
app.listen(port, (err) => {
if (err) {
console.log(err);
}
console.log(`Donation Web API listening on port ${port}`);
});