-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
59 lines (49 loc) · 1.5 KB
/
app.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
import mongoose from 'mongoose';
import util from 'util';
import envConfig from './env';
import app from './config/express';
const debug = require('debug')('promised-rest:Settings/App');
const port = process.env.PORT || envConfig.port;
const env = process.env || envConfig.env;
// make bluebird default Promise
Promise = require('bluebird'); // eslint-disable-line no-global-assign
Promise.config({
// Enable cancellation
cancellation: true,
});
// plugin bluebird promise in mongoose
mongoose.Promise = Promise;
const options = {
useNewUrlParser: true,
poolSize: 10
};
// connect to mongo db
mongoose.connect(envConfig.db, options);
mongoose.connection.on('error', () => {
throw new Error(`unable to connect to database: ${envConfig.db}`);
});
// print mongoose logs in dev env
// print mongoose logs in dev env
if (envConfig.MONGOOSE_DEBUG) {
mongoose.set('debug', (collectionName, method, query, doc) => {
debug(`${collectionName}.${method}`, util.inspect(query, false, 20), doc);
});
}
app.disable('etag');
// module.parent check is required to support mocha watch
// src: https://github.com/mochajs/mocha/issues/1912
if (!module.parent) {
// listen on port port
app.listen(port, () => {
debug(`promised-rest server started on port ${port} (${env})`);
});
}
/*
process.on('SIGINT', function(){
mongoose.connection.close(function(){
console.log("Mongoose default connection is disconnected due to application termination");
process.exit(0);
});
});
*/
export default app;