This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
loadApp.js
45 lines (37 loc) · 1.42 KB
/
loadApp.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
var fs = require('fs');
var debug = require('debug')('express-chat');
module.exports = function(userconfig, callback, bindPort) {
callback = callback || function nocallback() {};
bindPort = bindPort !== false;
var model = {};
var app = {};
var config = require('./config')(userconfig);
// we need to set NODE_ENV before calling require('express')
// so before require('express-autoload')
if(config.settings.env) process.env.NODE_ENV = config.settings.env;
debug('Starting application...');
debug('Configuration is :\n', config);
require('./autoload')(app, model, config, function(err) {
if(err) return callback(err);
app.libs.Step(function() {
var next = this;
if(bindPort) {
app.server.listen(config.port, config.hostname, next);
app.server.once("error", function(err) {
console.error("error", err);
next(err);
});
} else {
next();
}
}, function(err) {
var closeApp = function() {
if(app.server._handle) app.server.close();
app.apps.forEach(function(clientApp) {
if(clientApp.server._handle) clientApp.server.close();
});
};
callback(err, app, model, config, closeApp);
});
});
};