-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
102 lines (86 loc) · 3.35 KB
/
app.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
var path = require('path');
var fs = require('fs');
var express = require('express');
var favicon = require('serve-favicon');
var http = require('http');
var https = require('https');
var forceSSL = require('express-force-ssl');
var app = express();
var proxy = require('http-proxy-middleware');
var config = require('./config');
var privateKey, certificate;
// Default Ports
var PORTS = {
HTTP: config.web.http.port || 80,
HTTPS: config.web.https.port || 443,
FORCE_SSL_PORT: ''
}
PORTS.FORCE_SSL_PORT = config.web.https.forceSSLPort || PORTS.HTTPS;
// #2 SSL Support. All triggerd by the presence of config.web.https
if (config.web.https.enabled){
console.log('Enabling HTTPS');
privateKey = fs.readFileSync(path.resolve(config.web.https.keyPath));
certificate = fs.readFileSync(path.resolve(config.web.https.certPath));
if (config.web.https.forceHttps) {
app.set('forceSSLOptions', {
enable301Redirects: true,
trustXFPHeader: false,
httpsPort: PORTS.FORCE_SSL_PORT,
sslRequiredMessage: 'SSL Required.'
});
app.use(forceSSL);
}// config.web.https.forceHttps
}// config.web.https
//Uncomment if you don't want to redirect / and /apex to the new /ords
if (config.ords.redirectPaths.length > 0){
for(i=0; i< config.ords.redirectPaths.length; i++){
app.use(config.ords.redirectPaths[i],function(req, res, next){
res.redirect(config.ords.path);
});
}
}
//Can store custom images in public/...
app.use(config.static.path, express.static(config.static.directory));
app.use(config.apex.images.path,express.static(config.apex.images.directory));
//Register favicon if applicable
// if (config.faviconUrl){
// console.log('Favicon')
// app.use(favicon(__dirname + config.faviconUrl));
// }
// https://github.com/chimurai/http-proxy-middleware
app.use(config.ords.path,proxy(
{
target: config.ords.webContainerUrl,
changeOrigin: false,
// Additional work seems to be required for unsigned certificats
onProxyReq: function(proxyReq, req, res) {
// For encrypted calls, if we don't set the origin on POST request then we'll get the following error
// The request cannot be processed because this resource does not support Cross Origin Sharing requests, or the request Origin is not authorized to access this resource. If ords is being reverse proxied ensure the front end server is propagating the host name, for mod_proxy ensure ProxyPreserveHost is set to On
if (req.connection.encrypted && req.headers.origin){
proxyReq.setHeader('origin', req.headers.origin.replace(/^https:/,'http:'));
}
}, //onProxyReq
onProxyRes: function(proxyRes, req, res){
// If encrypted and headers['location'] exists (doesn't happen on some redirects)
if (req.connection.encrypted && proxyRes.headers['location']){
proxyRes.headers['location'] = proxyRes.headers['location'].replace(/^http:/g,'https:');
}
} // onProxyRes
}
));
// Make sure this is last as it will forward to APEX
app.get('/', function(req, res, next){
// console.log('in / forward');
// console.log('req.headers.origin:', req.headers);
res.redirect(config.ords.path);
});
// app.listen(httpPort);
http.createServer(app).listen(PORTS.HTTP);
if (config.web.https.enabled){
https.createServer(
{
key: privateKey,
cert: certificate
},
app).listen(PORTS.HTTPS);
}// config.web.https