Last active
August 29, 2015 14:10
-
-
Save dongliu/70874c0a658d87b4cd3b to your computer and use it in GitHub Desktop.
reverse proxy express
This file contains 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
/** | |
* @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