-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
134 lines (108 loc) · 3.99 KB
/
server.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const querystring = require('querystring');
const url = require('url');
var extend = require('util')._extend;
var session = require('express-session');
const conf = require('./conf.js');
///var host = 'occinterface.herokuapp.com'
var port = process.env.PORT || 3000;
var proxyDefaultTarget = conf.backendURL;
//we define the backendURL as a gloabl variable
// window.backendURL = proxyDefaultTarget;
var proxyOptions = {
path : '/proxiedOCCIServer/**',
target: proxyDefaultTarget,
targetPath: '',
rewrite: function(req) {
req.url = req.url.replace(new RegExp('^\/proxiedOCCIServer'), this.targetPath);
console.log('proxying to url', req.url, this)
},
changeOrigin: true
};
proxyOptions.rewrite.bind(proxyOptions); // to be able to access this.targetPath within itself
function updateProxyConf(myProxyOptions, req) {
var target = querystring.parse(req._parsedUrl.query).proxyTarget;
var targetPath = url.parse(target).pathname;
if (proxyOptions.targetPath.endsWith('/')) {
targetPath = targetPath.slice(0, -1);
}
myProxyOptions.target = target;
myProxyOptions.targetPath = targetPath;
}
function setupProd(app) {
// update the proxied target URL :
// test using ex. http://localhost:3000/conf?proxyTarget=http://malmo.lizenn.net:8080 then http://localhost:3000/proxiedOCCIServer/-/
app.all('/conf', function(req, res) {
//if the proxyOptions does not exist yet, we create it
if(!req.session.proxyOptions){
req.session.proxyOptions = extend(proxyOptions); // (deep) clone
}
updateProxyConf(req.session.proxyOptions, req);
console.log('updated proxyOptions via current session', req.session.proxyOptions);
console.log(' id of session: '+req.session.id);
res.setHeader('Content-Type', 'application/javascript');
res.end('{}');
});
}
function setupDev(app) {
// update the proxied target URL :
// test using ex. http://localhost:3000/conf?proxyTarget=http://malmo.lizenn.net:8080 then http://localhost:3000/proxiedOCCIServer/-/
app.all('/conf', function(req, res) { // TODO only post ?
updateProxyConf(proxyOptions, req);
console.log('updated proxyOptions', proxyOptions);
res.setHeader('Content-Type', 'application/javascript');
res.end('{}');
});
}
var isProduction = process.env.NODE_ENV === 'production';
console.log('env is prod =', isProduction);
if (isProduction) { // prod :
var express = require('express');
var http = require('http');
var httpProxy = require('http-proxy');
var proxy = new httpProxy.createProxyServer({secure: false});
// Init express server
var app = this.app = new express();
app.use(express.static(__dirname + '/'));
//we make a session for each new user
app.use(session({
secret: 'keyboard cat'
}));
app.all(proxyOptions.path, function (req, res, next) {
proxyOptions.rewrite(req, req.session.proxyOptions);
proxy.web(req, res, req.session.proxyOptions, function(err){
var msg = 'cannot proxy to ' + req.session.proxyOptions.target + '('+ err.message + ')';
console.log('proxy-error'+msg);
res.statusCode = 502;
res.end();
}.bind(this));
}.bind(this));
//when getting /conf request
setupProd(app);
//for react router, we redirect on index.html
app.get('*', function (request, response){
response.sendFile(__dirname+'/index.html');
})
var listeningApp = http.createServer(app);
listeningApp.listen(port, function (err, result) {
if(err) {
return console.log(err);
}
});
console.log(`Listening at http://localhost:${port}`);
} else { // dev :
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.config');
new WebpackDevServer(webpack(config), {
publicPath: config.output.publicPath,
hot: true,
historyApiFallback: true,
proxy: [proxyOptions],
setup: setupDev
}).listen(port, function (err, result) {
if (err) {
return console.log(err);
}
console.log('Listening at http://localhost:' + port + '/');
});
}