-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
50 lines (40 loc) · 1.26 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
'use strict';
require('./config/init')();
let config = require('./config/config'),
mongoose = require('mongoose');
mongoose.Promise = global.Promise;
/**
* Main application entry file.
* Please note that the order of loading is important.
*/
const conn = mongoose.connection;
conn.on('connecting', function() {
console.log('Connecting to MongoDB...');
});
conn.on('error', function(error) {
console.error('Error in MongoDB connection: ' + error);
mongoose.disconnect();
});
conn.on('connected', function() {
console.log('Connected to MongoDB.');
});
conn.once('open', function() {
console.log('Connection to MongoDB open.');
});
conn.on('reconnected', function () {
console.log('Reconnected to MongoDB');
});
conn.on('disconnected', function() {
console.log('Disconnected from MongoDB.');
console.log('DB URI is: ' + config.db);
mongoose.connect(config.db, config.dbOptions);
});
const db = mongoose.connect(config.db, config.dbOptions);
// Init the express application
const app = require('./config/express')(db);
// Start the app by listening on <port>
app.listen(config.port, config.ip);
// Expose app
exports = module.exports = app;
// Logging initialization
console.log('API started on port ' + config.port + ' with IP ' + config.ip);