-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.js
More file actions
59 lines (56 loc) · 1.72 KB
/
Copy pathdb.js
File metadata and controls
59 lines (56 loc) · 1.72 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var mongoose = require('mongoose');
var gracefulShutdown;
var dbURI = 'mongodb://localhost/PakCoin';
// remove comments when shifted to Heroku
/*if (process.env.NODE_ENV === 'production') {
dbURI = process.env.MONGOLAB_URI;
}*/
// CONNECTION EVENTS
mongoose.connection.on('connected', function() {
console.log('App: Mongoose connected to ' + dbURI);
});
mongoose.connection.on('error', function(err) {
console.log('App: Mongoose connection error: ' + err);
});
mongoose.connection.on('disconnected', function() {
console.log('App: Mongoose disconnected');
});
// CAPTURE APP TERMINATION / RESTART EVENTS
// To be called when process is restarted or terminated
gracefulShutdown = function(msg, callback) {
mongoose.connection.close(function() {
console.log('App: Mongoose disconnected through ' + msg);
callback();
});
};
// For nodemon restarts
process.once('SIGUSR2', function() {
gracefulShutdown('nodemon restart', function() {
process.kill(process.pid, 'SIGUSR2');
});
});
// For app termination
process.on('SIGINT', function() {
gracefulShutdown('app termination', function() {
process.exit(0);
});
});
// For Heroku app termination
process.on('SIGTERM', function() {
gracefulShutdown('Heroku app termination', function() {
process.exit(0);
});
});
// Attempt to connect to mongodb
// Show appropriate log on success or failure to connect
// Parameters from app.js
module.exports = function(app, port){
mongoose.connect(dbURI, function(err){
app.listen(port, function(){
if(err)
console.log(`App: Server could not start on port ${port}`)
else
console.log(`App: Server started on port ${port}`)
})
})
}