http-proxy-middleware = function (context, opts) { return new HPM(context, opts); }
n/a
function createConfig(context, opts) { // structure of config object to be returned var config = { context: undefined, options: {} }; // app.use('/api', proxy({target:'http://localhost:9000'})); if (isContextless(context, opts)) { config.context = '/'; config.options = _.assign(config.options, context); } // app.use('/api', proxy('http://localhost:9000')); // app.use(proxy('http://localhost:9000/api')); else if (isStringShortHand(context)) { var oUrl = url.parse(context); var target = [oUrl.protocol, '//', oUrl.host].join(''); config.context = oUrl.pathname || '/'; config.options = _.assign(config.options, {target: target}, opts); if (oUrl.protocol === 'ws:' || oUrl.protocol === 'wss:') { config.options.ws = true; } // app.use('/api', proxy({target:'http://localhost:9000'})); } else { config.context = context; config.options = _.assign(config.options, opts); } configureLogger(config.options); if (!config.options.target) { throw new Error('[HPM] Missing "target" option. Example: {target: "http://www.example.org"}'); } // Legacy option.proxyHost config.options = mapLegacyProxyHostOption(config.options); // Legacy option.proxyTable > option.router config.options = mapLegacyProxyTableOption(config.options); return config; }
n/a
function matchContext(context, uri, req) { // single path if (isStringPath(context)) { return matchSingleStringPath(context, uri); } // single glob path if (isGlobPath(context)) { return matchSingleGlobPath(context, uri); } // multi path if (Array.isArray(context)) { if (context.every(isStringPath)) { return matchMultiPath(context, uri); } if (context.every(isGlobPath)) { return matchMultiGlobPath(context, uri); } throw new Error('[HPM] Invalid context. Expecting something like: ["/api", "/ajax"] or ["/api/**", "!**.html"]'); } // custom matching if (_.isFunction(context)) { var pathname = getUrlPathName(uri); return context(pathname, req); } throw new Error('[HPM] Invalid context. Expecting something like: "/api" or ["/api", "/ajax"]'); }
...
For full control you can provide a custom function to determine which requests should be proxied or not.
```javascript
/**
* @return {Boolean}
*/
var filter = function (pathname, req) {
return (pathname.match('^/api') && req.method === 'GET
');
};
var apiProxy = proxy(filter, {target: 'http://www.example.org'})
```
## Options
...
function getProxyEventHandlers(opts) { // https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events var proxyEvents = ['error', 'proxyReq', 'proxyReqWs', 'proxyRes', 'open', 'close']; var handlers = {}; _.forEach(proxyEvents, function(event) { // all handlers for the http-proxy events are prefixed with 'on'. // loop through options and try to find these handlers // and add them to the handlers object for subscription in init(). var eventName = _.camelCase('on ' + event); var fnHandler = _.get(opts, eventName); if (_.isFunction(fnHandler)) { handlers[event] = fnHandler; } }); // add default error handler in absence of error handler if (!_.isFunction(handlers.error)) { handlers.error = defaultErrorHandler; } // add default close handler in absence of close handler if (!_.isFunction(handlers.close)) { handlers.close = logClose; } return handlers; }
n/a
function init(proxy, opts) { var handlers = getProxyEventHandlers(opts); _.forIn(handlers, function(handler, eventName) { proxy.on(eventName, handlers[eventName]); }); logger.debug('[HPM] Subscribed to http-proxy events: ', _.keys(handlers)); }
n/a
function getArrow(originalPath, newPath, originalTarget, newTarget) { var arrow = ['>']; var isNewTarget = (originalTarget !== newTarget); // router var isNewPath = (originalPath !== newPath); // pathRewrite if (isNewPath && !isNewTarget) {arrow.unshift('~');} else if (!isNewPath && isNewTarget) {arrow.unshift('=');} else if (isNewPath && isNewTarget) {arrow.unshift('≈');} else {arrow.unshift('-');} return arrow.join(''); }
n/a
getInstance = function () { if (!loggerInstance) { loggerInstance = new Logger(); } return loggerInstance; }
...
var _ = require('lodash');
var url = require('url');
var logger = require('./logger').getInstance();
module.exports = {
createConfig: createConfig
};
function createConfig(context, opts) {
// structure of config object to be returned
...
function createPathRewriter(rewriteConfig) { var rulesCache; if (!isValidRewriteConfig(rewriteConfig)) { return; } if (_.isFunction(rewriteConfig)) { var customRewriteFn = rewriteConfig; return customRewriteFn; } else { rulesCache = parsePathRewriteRules(rewriteConfig); return rewritePath; } function rewritePath(path) { var result = path; _.forEach(rulesCache, function(rule) { if (rule.regex.test(path)) { result = result.replace(rule.regex, rule.value); logger.debug('[HPM] Rewriting path from "%s" to "%s"', path, result); return false; } }); return result; } }
n/a
function getTarget(req, config) { var newTarget; var router = config.router; if (_.isPlainObject(router)) { newTarget = getTargetFromProxyTable(req, router); } else if (_.isFunction(router)) { newTarget = router(req); } return newTarget; }
n/a