-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (59 loc) · 1.84 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
/**
* Created by karl on 16/07/15.
*/
/// <reference path='typings/tsd.d.ts' />
'use strict';
var http = require('http');
var cluster = require('cluster');
var express = require('express');
var compress = require('compression');
var eJSend = require('easy-jsend');
var eXHeaders = require('easy-x-headers');
var Common = require('./lib/common');
var Log = require('./lib/log');
var Error = require('./lib/error');
var clusterService = require('cluster-service');
var e;
function init(config) {
e = config.express || express;
eJSend.init(config.jSend);
}
exports.init = init;
function create(config) {
var app = express();
var log = Log.init(config.log);
if (config.cluster) {
config.cluster.port = config.cluster.port || config.port + 10000;
}
if (!config.cluster || !cluster.isMaster || config.forceRegister) {
Common.register(app, config.root, config.cluster);
app.use(compress());
app.set('trust proxy', true);
app.use(eXHeaders.getMiddleware(config.xHeaderDefaults));
app.use(log.middleware());
config.mount(app);
app.all('*', function (req, res, next) {
res.fail('Not found', 404);
});
var server = http.createServer(app);
app.use(Error.getErrorHandler(server));
}
return {
express: express,
app: app,
log: log.log,
server: server,
listen: function () {
if (config.cluster && cluster.isMaster) {
config.cluster.workers = process['mainModule'].filename;
clusterService.start(config.cluster);
return;
}
server.listen(config.port, function () {
log.log.info('%s listening on %d', config.name, config.port);
});
}
};
}
exports.create = create;
//# sourceMappingURL=index.js.map