-
Notifications
You must be signed in to change notification settings - Fork 157
/
server.js
41 lines (34 loc) · 1.48 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
var express = require('express'),
request = require('request'),
bodyParser = require('body-parser'),
app = express();
var myLimit = typeof(process.argv[2]) != 'undefined' ? process.argv[2] : '100kb';
console.log('Using limit: ', myLimit);
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.header('Target-URL');
if (!targetURL) {
res.send(500, { error: 'There is no Target-Endpoint header in the request' });
return;
}
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)
}
// console.log(body);
}).pipe(res);
}
});
app.set('port', process.env.PORT || 3000);
app.listen(app.get('port'), function () {
console.log('Proxy server listening on port ' + app.get('port'));
});