Skip to content

Instantly share code, notes, and snippets.

@dongliu
Last active August 29, 2015 14:10
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 dongliu/70874c0a658d87b4cd3b to your computer and use it in GitHub Desktop.
Save dongliu/70874c0a658d87b4cd3b to your computer and use it in GitHub Desktop.
reverse proxy express
/**
* @fileOverview a middleware for express deployment behind a reverse proxy
* @param {string} proxy - the known reverse proxy host
* @param {string} proxied_service - the url assigned by proxy
* @return {function} the middleware
* /
function proxied (proxy, proxied_service) {
return function (req, res, next) {
req.proxied = false;
req.proxied_prefix = '';
res.locals.proxied_prefix = '';
if (req.get('x-forwarded-host') && req.get('x-forwarded-host') === proxy) {
// req.proxied and req.proxied_prefix can be used in routes
req.proxied = true;
req.proxied_prefix = url.parse(proxied_service).pathname;
// res.locals.prefix can be used in render
res.locals.proxied_prefix = url.parse(proxied_service).pathname;
}
next();
};
}
module.exports = {
proxied: proxied
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment