-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdev-proxy.js
46 lines (38 loc) · 1.67 KB
/
dev-proxy.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
var express = require('express'),
request = require('request'),
bodyParser = require('body-parser');
function createProxy(targetURL, port) {
var app = express();
var myLimit = typeof(process.argv[2]) != 'undefined' ? process.argv[2] : '500kb';
console.log('Using limit: ', myLimit);
app.use(bodyParser.json({limit: myLimit}));
app.all('*', function (req, res, next) {
// allow CORS
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 {
if (!targetURL) {
res.status(500).send({ error: 'There is no Target-Endpoint header in the request' });
return;
}
console.log("API CALL: " + req.method + "\t" + targetURL + req.url);
request({ url: targetURL + req.url, method: req.method, json: req.body, headers: {'Authorization': req.header('Authorization')} },
function (error, response, body) {
if (error) {
console.error('error: ' + response.statusCode);
}
}).pipe(res);
}
});
app.set('port', port);
app.listen(app.get('port'), function () {
console.log('Proxy server listening on port ' + app.get('port'));
});
}
// Create two proxy instances with different target URLs and ports
createProxy("https://api.qubic.li", 7003);
createProxy("https://testapi.qubic.org", 7004);