-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (29 loc) · 959 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
const port = process.env.PORT || 2525;
const dbRetryTime = process.env.db_retry_time || 2000;
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const router = require('./services/router');
const mongoose = require('mongoose');
const MONGO_PORT = 27017;
const mongoUri = `mongodb://${process.env.db_service_name}:${MONGO_PORT}/${process.env.db_name}`;
let db = mongoose.connection;
let connectWithRetry = function () {
return mongoose.connect(mongoUri, {
useUnifiedTopology: true,
useNewUrlParser: true
});
};
connectWithRetry();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
db.on('error', () => {
setTimeout(() => {
console.log('DB connection failed. Will try again.');
connectWithRetry();
}, dbRetryTime);
});
db.on('connected', function () {
app.use(router);
app.listen(port, () => console.log(`All set up. Listening on ${port}!`))
});