-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (44 loc) · 1023 Bytes
/
index.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
const { port, env } = require('./config');
const logger = require('./config/logger');
const app = require('./config/express');
const { connect } = require('./config/mongoose');
const google = require('./src/utils/google');
function listen() {
app.listen(port || 5000, '0.0.0.0', () => {
logger.info(`server listening on port ${port || 5000} (${env})`);
google.initialize();
});
}
const connection = connect();
let connected = false;
const waitForConnection = () =>
new Promise((resolve) => {
if (!connected) {
const interval = setInterval(() => {
if (connected) {
clearInterval(interval);
resolve();
}
}, 1000);
} else {
resolve();
}
});
connection
.on('open', () => {
connected = true;
})
.on('disconnected', () => {
connected = false;
})
.on('error', (err) => {
logger.error(`${err}`);
process.exit(-1);
});
waitForConnection().then(listen);
/**
*
* Exports express
* @public
*/
module.exports = app;