-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
64 lines (58 loc) · 2.35 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
62
63
64
'use strict';
var fs = require('fs');
var path = require('path');
var http = require('http');
var https = require('https');
var flags = require('node-flags');
var app = module.exports = require('./server/server');
var logger = require('./common/utils/logger');
if (require.main === module) {
let options = {}
if (process.env.NOW) {
process.env.NODE_ENV = 'production';
}
if (flags.get('adminUser') && flags.get('adminPass') && flags.get('adminEmail')) {
options.adminCredentials = {
username: flags.get('adminUser'),
password: flags.get('adminPass'),
email: flags.get('adminEmail')
}
}
app.boot(options)
.then(() => {
let httpOnly = process.env.HTTP;
let port = flags.get('port') || app.get('port');
if (typeof httpOnly === 'undefined') httpOnly = false;
let server = null;
// If http-only server requested, or running in ZEIT cloud, use http-server. ZEIT uses https, but
// uses its own front server, and deploys app in a background machine.
if (httpOnly || process.env.NOW) {
server = http.createServer(app);
} else {
let options = {
key: fs.readFileSync(path.join('.', 'private', 'privatekey.pem')),
cert: fs.readFileSync(path.join('.', 'private', 'certificate.pem'))
};
server = https.createServer(options, app);
}
server.listen(port, () => {
let baseUrl = `${httpOnly ? 'http' : 'https'}://${app.get('host')}:${port}`;
app.emit('started', baseUrl);
if (app.get('env') === 'development' || app.get('env') === 'test') {
console.log('Web server listening at: %s', baseUrl);
if (app.get('loopback-component-explorer')) {
let explorerPath = app.get('loopback-component-explorer').mountPath;
console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
}
} else if (app.get('env') === 'production') {
console.log('Production IoT hub started at : %s', baseUrl);
}
});
}, (err) => {
if (process.env.NODE_ENV === 'development') {
logger.error(`Error: ${err.stack}`);
} else {
logger.error(`Error: ${err.message}`);
}
});
}