-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
73 lines (52 loc) · 1.77 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
'use strict';
const express = require('express');
const httpProxy = require('http-proxy');
const https = require('https');
const fs = require('fs');
const conf = require('./conf.js');
const port = process.argv[2] || 8000;
const appName = "/";
const app = express();
/**********************************
/* PROXY for routing non-CORS API calls
*/
const httpsAgent = new https.Agent({ keepAlive:true, maxSockets:10 });
const proxyOptions = {
changeOrigin: true,
target: `https://${conf.client}.groupbycloud.com`,
agent: httpsAgent
};
const apiProxy = httpProxy.createProxyServer(proxyOptions);
httpProxy.prototype.onError = function (err) {
console.log(err);
};
const logPost = function(request){
if(request.method !== 'POST')
return;
let body = "";
request.on('data', function (chunk) {body += chunk;});
request.on('end', function () { console.log('POSTED DATA: ' + body);});
};
apiProxy.on('proxyReq', (proxyReq, req, res, options) => {
proxyReq.setHeader('Authorization', conf.key);
logPost(req);
});
apiProxy.on('proxyRes', (proxyRes, req, res, options) => {
console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2));
});
app.use(appName, express.static(__dirname + '/src/public/'));
app.listen(port, function() {
console.log('Express server listening on port ' + port);
});
//all requests route through here!
app.all('*', function(req, res) {
console.log(req.url);
if(req.url.startsWith('/wisdom/')){
apiProxy.web(req, res);
//console.log( JSON.stringify(req.headers, null, '\t') );
res.on('finish', function() { console.log("The proxy request was completed"); });
return;
}
console.log("Sending request " + req.url + " to app" );
res.sendFile(__dirname + '/src/public/index.html');
});