-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·68 lines (55 loc) · 1.98 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const expressHttpContext = require('express-http-context');
const util = require('util');
// const mongoose = require('mongoose');
const logger = require('./config/logger');
const config = require('./config/config');
const app = require('./config/express');
logger.debug('logger debug level is on.');
// make bluebird default Promise
Promise = require('bluebird'); // eslint-disable-line no-global-assign
/*
// plugin bluebird promise in mongoose
mongoose.Promise = Promise;
// Todo - Add cache to mongoose
// connect to mongo db
const { mongoUri } = config;
mongoose.set('useCreateIndex', true);
mongoose.connect(mongoUri, {
useNewUrlParser: true,
poolSize: 2,
// ssl: true, // Todo - set on in prod
keepAlive: 300000,
connectTimeoutMS: 30000,
autoReconnect: true,
reconnectTries: 300000,
reconnectInterval: 5000,
promiseLibrary: global.Promise,
});
mongoose.connection.on('error', () => {
throw new Error(`unable to connect to database: ${mongoUri}`);
});
logger.info(`Mongoose is connected to ${mongoUri}`);
// print mongoose logs in dev env
if (config.mongooseDebug) {
mongoose.set('debug', (collectionName, method, query, doc, options) => {
const opts = options ? ` opts[${JSON.stringify(options)}]` : '';
logger.info(`mongoose - ${collectionName}.${method}${opts}`, JSON.stringify(util.inspect(query, false, 20)), JSON.stringify(doc));
});
}
*/
const init = async () => {
logger.info('pre-listener async tasks, e.g. get data from server...'); // eslint-disable-line no-console
// ....
logger.info('init done.');
};
const listener = async () => {
// module.parent check is required to support mocha watch
// src: https://github.com/mochajs/mocha/issues/1912
if (!module.parent) {
app.listen(config.port, () => {
logger.info(`server started listening on port ${config.port} (${config.env})`); // eslint-disable-line no-console
});
}
};
init().then(() => listener());
module.exports = app;