-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
60 lines (54 loc) · 1.46 KB
/
index.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
var app = require('express')();
var request = require('request');
var port = process.env.PORT || 3000;
var urlparam = process.env.URLPARAM || 'url';
app.use(function (req, res, next) {
var data='';
req.setEncoding('utf8');
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function() {
req.body = data;
next();
});
});
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
next();
});
app.options('*', function (req, res) {
res.send();
});
app.use(function (req, res, next) {
if (req.query[urlparam]) {
var options = {
url: req.query[urlparam],
method: req.method,
headers: req.headers,
qs: req.query
};
if (['PUT', 'POST', 'PATCH'].indexOf(req.method) > -1) {
options.body = req.body;
}
delete options.qs[urlparam];
delete options.headers.host;
delete options.headers.origin;
request(options)
.on('error', console.log)
.on('response', function (response) {
console.log('----------------');
console.log('HEADERS');
console.log('----------------');
console.log(response.headers);
console.log('----------------');
console.log('STATUS CODE: ', response.statusCode);
console.log('----------------');
console.log('STATUS MESSAGE', response.statusMessage);
console.log('----------------');
})
.pipe(res);
}
});
app.listen(port, console.log);