-
Notifications
You must be signed in to change notification settings - Fork 37
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,15 @@ | |
|
||
const request = require('request-promise'); | ||
const converter = require('rel-to-abs'); | ||
const url = require('url'); | ||
const util = require('util'); | ||
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to preserve the user agent above. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -53,4 +64,4 @@ module.exports = function(app){ | |
return res.send(originResponse.message); | ||
}); | ||
}); | ||
}; | ||
}; |
There was a problem hiding this comment.
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.