forked from alessioalex/ClientManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·74 lines (65 loc) · 2.13 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var connect = require('connect'),
express = require('express'),
connectTimeout = require('connect-timeout'),
mongoose = require('mongoose'),
gzippo = require('gzippo'),
utils = require('./lib/utils'),
EventEmitter = require('events').EventEmitter,
AppEmitter = new EventEmitter(),
app = express.createServer(),
ENV = process.env.NODE_ENV || 'development',
log = console.log,
dbPath;
utils.loadConfig(__dirname + '/config', function(config) {
app.use(function(req, res, next) {
res.removeHeader("X-Powered-By");
next();
});
app.configure(function() {
utils.ifEnv('production', function() {
// enable gzip compression
app.use(gzippo.compress());
});
app.use(express.favicon());
app.use(express['static'](__dirname + '/public'));
app.use(express.bodyParser());
app.use(express.methodOverride());
utils.ifEnv('production', function() {
app.use(connectTimeout({
time: parseInt(config[ENV].REQ_TIMEOUT, 10)
}));
});
});
mongoose = utils.connectToDatabase(mongoose, config.db[ENV].main);
// register models
require('./app/models/client')(mongoose);
// register controllers
['clients', 'errors'].forEach(function(controller) {
require('./app/controllers/' + controller + '_controller')(app, mongoose, config);
});
app.on('error', function (e) {
if (e.code == 'EADDRINUSE') {
log('Address in use, retrying...');
setTimeout(function () {
app.close();
app.listen(config[ENV].PORT, function() {
app.serverUp = true;
});
}, 1000);
}
});
if (!module.parent) {
app.listen(config[ENV].PORT, function() {
app.serverUp = true;
});
log('Express server listening on port %d, environment: %s', app.address().port, app.settings.env);
}
AppEmitter.on('checkApp', function() {
AppEmitter.emit('getApp', app);
});
});
/**
* export AppEmitter for external services so that the callback can execute
* when the app has finished loading the configuration
*/
module.exports = AppEmitter;