This repository was archived by the owner on Aug 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (44 loc) · 1.61 KB
/
index.js
File metadata and controls
57 lines (44 loc) · 1.61 KB
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
var express = require('express');
var request = require('request');
var url = require('url');
var compression = require('compression');
var port = (process.env.VCAP_APP_PORT || 3000);
var app = express();
app.use(compression());
app.use(express.static(__dirname + '/public'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.get('/', function(req, res){
res.render(__dirname + '/public/index.html', {});
});
app.all('/api/request', function(req, res) {
var targetUrl = url.parse(req.headers['target-url']);
// Remove headers we don't want shipped with the request
delete req.headers['target-url'];
delete req.headers['referer']; // Support the sometimes wrongly spelled version
delete req.headers['referrer'];
delete req.headers['accept-encoding']; // This can cause problems, since we don't handle gzip well
// Transform some headers to match the request we're sending
req.headers['host'] = targetUrl.host;
var proxy = request({
method: req.method,
url: targetUrl,
headers: req.headers,
body: req.body
}, function(err, resp, body) {
if (err) {
console.error('Request returned an error: ' + err);
res.status(500).send(err);
return;
}
for (var key in resp.headers) {
if (!resp.headers.hasOwnProperty(key)) {
continue;
}
res.setHeader(key, resp.headers[key]);
}
res.status(resp.statusCode).send(body);
});
});
app.listen(port);
console.log('Postman backend now running on port ' + port);