-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
64 lines (54 loc) · 2.01 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
/**
* Initialize HTTP and HTTPS servers for Ember app
*/
var ranger = require('park-ranger')();
var compression = require('compression'),
cors = require('cors'),
debug = require('debug')('ember-server'),
express = require('express'),
fastbootMiddleware = require('fastboot-express-middleware'),
http = require('http'),
https = require('https'),
path = require('path'),
serveStatic = require('serve-static'),
app = express();
let httpsPort = process.env.EMBER_SERVER_HTTPS_PORT ? process.env.EMBER_SERVER_HTTPS_PORT : 8124;
let httpPort = process.env.EMBER_SERVER_HTTP_PORT ? process.env.EMBER_SERVER_HTTP_PORT : 8123;
app.use(compression());
app.use(cors());
if (!process.env.EMBER_SERVER_APP_DIR) {
throw new Error('No app directory found in environment');
}
['.well-known', 'assets', 'bower_components', 'images', 'manifest.json'].forEach((directory) => {
app.use(`/${directory}`, serveStatic(path.resolve(process.env.EMBER_SERVER_APP_DIR, `${directory}`)));
});
/**
* Redirect all HTTP requests to HTTPS
*/
app.use(function(req, res, next) {
if (req.secure || req.headers.host.substr(0, req.headers.host.indexOf(':')) === 'localhost') {
next();
} else {
// Change port if host includes one explicitly
if (req.headers.host.includes(':')) {
res.redirect(`https://${req.headers.host.substr(0, req.headers.host.indexOf(':'))}:${httpsPort}${req.url}`);
} else {
res.redirect(`https://${req.headers.host}${req.url}`);
}
}
});
if (process.env.EMBER_SERVER_FASTBOOT === 'true') {
app.get('*', fastbootMiddleware(process.env.EMBER_SERVER_APP_DIR));
} else {
app.get('*', function(req, res) {
res.sendFile(path.resolve(process.env.EMBER_SERVER_APP_DIR, 'index.html'));
});
}
if (ranger.cert) {
https.createServer(ranger.cert, app).listen(httpsPort, () => {
debug('Ember server started listening for HTTPS requests', { port: httpsPort});
});
}
http.createServer(app).listen(httpPort, () => {
debug('Ember server started listening for HTTP requests', { port: httpPort });
});