-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
70 lines (56 loc) · 1.91 KB
/
server.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
'use strict';
var express = require('express');
var kraken = require('kraken-js');
var http = require('http');
var db = require('./lib/database');
var sprintf = require('sprintf');
var options, app, server, logger, conf;
console.log(require('./lib/logo'));
/*
* Create and configure application. Also exports application instance for use by tests.
* See https://github.com/krakenjs/kraken-js#options for additional configuration options.
*/
options = {
onconfig: function (config, next) {
conf = config;
require('./lib/config').init(config);
// allow command line switch from serving /dist to /app
if( config.get('dev') ) {
var middleware = config.get('middleware').static;
middleware.module.arguments[0] = middleware.module.arguments[0].replace(/dist$/,'public');
}
db.init(config, function(err, database) {
next(null, config);
onReady(config);
});
}
};
app = express();
app.use(kraken(options));
app.on('start', function () {
console.log(sprintf('%-40.40s%10s', 'Server Root:', conf.get('middleware').static.module.arguments[0]));
});
/*
* Create and start HTTP server.
*/
function onReady(config) {
server = http.createServer(app);
var devCon = require('./lib/dev');
if( conf.get('dev') || config.get('local') ) {
devCon.init(server, app);
} else {
devCon.prod(app);
}
server.listen(config.get('server').port || process.env.PORT || 8000);
server.on('listening', function () {
console.log(sprintf('%-40.40s%10s', 'Server Url:', `http://localhost:${this.address().port}`));
if( (conf.get('dev') || conf.get('local')) && !config.get('noautostart') ) {
var spawn = require('child_process').spawn
if( process.platform === 'linux' ) {
spawn('xdg-open', [`http://localhost:${this.address().port}`]);
} else {
spawn('open', [`http://localhost:${this.address().port}`]);
}
}
});
}