Created
March 15, 2016 15:53
-
-
Save jonstorer/86ac53dd22ffc862c140 to your computer and use it in GitHub Desktop.
proxy requests in node/express with an access token.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'); | |
var url = require('url'); | |
var proxy = require('express')(); | |
var buildUrl = function (path, query) { | |
var uri = url.parse(config.api.host); | |
uri.pathname = path; | |
if(query) { uri.search = "?" + query }; | |
return url.format(uri); | |
}; | |
proxy.all('*', function (req, res, next){ | |
if (req.session.access_token) { | |
req.headers['Authorization'] = "Bearer " + req.session.access_token; | |
} | |
var currentUrl = url.parse(req.url); | |
var newUrl = buildUrl(currentUrl.pathname, currentUrl.query); | |
req.pipe(request(newUrl)).pipe(res); | |
}); | |
module.exports = proxy; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment