-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
63 lines (54 loc) · 2.25 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
var express = require('express'),
request = require('request'),
bodyParser = require('body-parser'),
app = express();
var myLimit = typeof(process.argv[2]) != 'undefined' ? process.argv[2] : '10000kb';
var myPort = process.env.PORT || 3000;;
console.log('Using limit: ', myLimit);
var urlPrefix = 'http://localhost:'+myPort+'/';
app.use(bodyParser.json({limit: myLimit}));
app.all('*', function (req, res, next) {
// Set CORS headers: allow all origins, methods, and headers: you may want to lock this down in a production environment
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, PATCH, POST, DELETE");
res.header("Access-Control-Allow-Headers", req.header('access-control-request-headers'));
if (req.method === 'OPTIONS') {
// CORS Preflight
res.send();
} else {
var targetURL = req.url.substring(1,req.url.length);
if (!targetURL) {
res.send(500, { error: 'There is no Target-Endpoint header in the request' });
return;
}
console.log(targetURL );
console.log(req.method);
console.log(req.header('Authorization'));
if (!req.header('Authorization'))
{
console.log('Requesting '+ targetURL + ' without Header');
request({ url: targetURL, method: req.method, json: req.body },
function (error, response, body) {
if (error) {
console.error('error: ' + response.statusCode)
}
// console.log(body);
}).pipe(res);
}
else
{
console.log('Requesting '+ targetURL + ' With Header');
request({ url: targetURL, method: req.method, json: req.body, headers: {'Authorization': req.header('Authorization')} },
function (error, response, body) {
if (error) {
console.error('error: ' + response.statusCode)
}
// console.log(body);
}).pipe(res);
}
}
});
app.set('port', myPort);
app.listen(app.get('port'), function () {
console.log('Proxy server listening on port ' + app.get('port'));
});