Skip to content

Instantly share code, notes, and snippets.

@iamdey
Created April 20, 2017 09:59
Show Gist options
  • Save iamdey/dd05f21d50a53ffadb324a9fbcb270eb to your computer and use it in GitHub Desktop.
Save iamdey/dd05f21d50a53ffadb324a9fbcb270eb to your computer and use it in GitHub Desktop.
const proxy = require('express-http-proxy');
const url = require('url');
module.exports = (app) => {
const { api: { hostname, port } } = app.get('coConfig');
const target = `http://${hostname}:${port}`;
app.use('/api', proxy(target, {
forwardPath: req => url.parse(req.originalUrl).path,
decorateRequest: (proxyReq, req) => {
if (!req.security) {
return;
}
// TODO refactor in Auth library may be or ...
const bearer = new Buffer(`${req.user.user_id}:${req.user.tokens.access_token}`)
.toString('base64');
proxyReq.headers.Authorization = `Bearer ${bearer}`;
console.log('proxyreq', proxyReq.headers);
},
intercept: (rsp, data, req, res, callback) => {
console.log('intercept', rsp.headers);
callback(null, JSON.stringify(data));
},
}));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment