-
Notifications
You must be signed in to change notification settings - Fork 37
/
bootstrap.js
60 lines (52 loc) · 2.26 KB
/
bootstrap.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
'use strict';
const request = require('request-promise');
const converter = require('rel-to-abs');
const fs = require('fs');
const index = fs.readFileSync('index.html', 'utf8');
const ResponseBuilder = require('./app/ResponseBuilder');
module.exports = app => {
app.get('/*', (req, res) => {
const responseBuilder = new ResponseBuilder(res);
const requestedUrl = req.url.slice(1);
const corsBaseUrl = '//' + req.get('host');
console.info(req.protocol + '://' + req.get('host') + req.url);
if(requestedUrl == ''){
res.send(index);
return;
}
request({
uri: requestedUrl,
resolveWithFullResponse: true,
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36'
}
})
.then(originResponse => {
responseBuilder
.addHeaderByKeyValue('Access-Control-Allow-Origin', '*')
.addHeaderByKeyValue('Access-Control-Allow-Credentials', false)
.addHeaderByKeyValue('Access-Control-Allow-Headers', 'Content-Type')
.addHeaderByKeyValue('X-Proxied-By', 'cors-container')
.build(originResponse.headers);
if(req.headers['rewrite-urls']){
res.send(
converter
.convert(originResponse.body, requestedUrl)
.replace(requestedUrl, corsBaseUrl + '/' + requestedUrl)
);
}else{
res.send(originResponse.body);
}
})
.catch(originResponse => {
responseBuilder
.addHeaderByKeyValue('Access-Control-Allow-Origin', '*')
.addHeaderByKeyValue('Access-Control-Allow-Credentials', false)
.addHeaderByKeyValue('Access-Control-Allow-Headers', 'Content-Type')
.addHeaderByKeyValue('X-Proxied-By', 'cors-containermeh')
.build(originResponse.headers);
res.status(originResponse.statusCode || 500);
return res.send(originResponse.message);
});
});
};