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 2 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
19 changes: 15 additions & 4 deletions bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

const request = require('request-promise');
const converter = require('rel-to-abs');
const url = require('url');
const util = require('util');
Copy link
Owner

Choose a reason for hiding this comment

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

I'd suggest removing this import and using basic concatenation for the string below.

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));
Copy link
Owner

Choose a reason for hiding this comment

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

Could this be switched into a full if statement with braces?

res.header(origin.headers);
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Credentials', false);
Expand All @@ -17,21 +21,28 @@ module.exports = function(app){
app.get('/*', (req, res) => {
let origionalUrl = req.originalUrl;
let requestedUrl = req.params[0];
let reqUrl = url.parse(requestedUrl)
Copy link
Owner

Choose a reason for hiding this comment

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

reqUrl isn't very informative or expressive to what this variable actually is, I think a better name would be formattedRequestUrl .

let corsBaseUrl = '//' + req.get('host');

console.info(req.protocol + '://' + req.get('host') + origionalUrl);
Copy link
Owner

Choose a reason for hiding this comment

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

Could we expand this also into an if with braces.


if(requestedUrl == ''){
res.send(index);
return;
}
if(!reqUrl.host) {
res.status(400);
res.send(util.format("Invalid Url '%s'",requestedUrl));
Copy link
Owner

Choose a reason for hiding this comment

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

Use standard concatenation here.

return
}

req.headers['host'] = reqUrl.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 +64,4 @@ module.exports = function(app){
return res.send(originResponse.message);
});
});
};
};