-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
61 lines (47 loc) · 1.57 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
60
61
'use strict';
var Q = require('q'),
path = require('path'),
caller = require('caller'),
express = require('express'),
bootstrap = require('./lib/bootstrap'),
debug = require('debuglog')('kraken');
function noop(obj, cb) {
cb(null, obj);
}
module.exports = function (options) {
var app;
if (typeof options === 'string') {
options = { basedir: options };
}
options = options || {};
options.protocols = options.protocols || {};
options.onconfig = options.onconfig || noop;
options.basedir = options.basedir || path.dirname(caller());
debug('kraken options\n', options);
app = express();
app.once('mount', function onmount(parent) {
var deferred, complete, start, error;
// Remove sacrificial express app
parent.stack.pop();
deferred = Q.defer();
complete = deferred.resolve.bind(deferred);
start = parent.emit.bind(parent, 'start');
error = parent.emit.bind(parent, 'error');
// Kick off server and add middleware which will block until
// server is ready. This way we don't have to block standard
// `listen` behavior, but failures will occur immediately.
bootstrap(parent, options)
.then(complete)
.then(start)
.catch(error)
.done();
parent.use(function startup(req, res, next) {
if (deferred.promise.isFulfilled()) {
next();
return;
}
res.send(503, 'Server is starting.');
});
});
return app;
};