Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed headers #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

const request = require('request-promise');
const converter = require('rel-to-abs');
const url = require('url');
const fs = require('fs');
const index = fs.readFileSync('index.html', 'utf8');
const debug = process.env.debug_log || false;

module.exports = function(app){
function setHeaders(res, origin){
if(debug) {
console.info("setHeaders origin:" , JSON.stringify(origin.headers));
}
res.header(origin.headers);
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Credentials', false);
Expand All @@ -15,23 +20,32 @@ module.exports = function(app){
}

app.get('/*', (req, res) => {
let origionalUrl = req.originalUrl;
let originalUrl = req.originalUrl;
let requestedUrl = req.params[0];
let parsedRequestUrl = url.parse(requestedUrl)
let corsBaseUrl = '//' + req.get('host');

console.info(req.protocol + '://' + req.get('host') + origionalUrl);
console.info(req.protocol + '://' + req.get('host') + originalUrl);

if(requestedUrl == ''){
res.send(index);
return;
}
if(!parsedRequestUrl.host) {
res.status(400);
res.send("Invalid Url '" + requestedUrl + "'");
return
}

req.headers['host'] = parsedRequestUrl.host;
if(debug) {
console.info("Req headers:" , JSON.stringify(req.headers));
}

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'
}
headers: req.headers
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to preserve the user agent above.

Copy link

@mrmx mrmx Apr 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's passed along within the original req.headers

})
.then(originResponse => {
setHeaders(res, originResponse);
Expand All @@ -53,4 +67,4 @@ module.exports = function(app){
return res.send(originResponse.message);
});
});
};
};