Skip to content

Instantly share code, notes, and snippets.

@jonstorer
Created March 15, 2016 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonstorer/86ac53dd22ffc862c140 to your computer and use it in GitHub Desktop.
Save jonstorer/86ac53dd22ffc862c140 to your computer and use it in GitHub Desktop.
proxy requests in node/express with an access token.
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