http-proxy-middleware
Advanced tools
Comparing version 0.8.2 to 0.9.0
22
index.js
@@ -8,2 +8,3 @@ var _ = require('lodash'); | ||
var ProxyTable = require('./lib/proxy-table'); | ||
var logger = require('./lib/logger').getInstance(); | ||
@@ -17,3 +18,3 @@ var httpProxyMiddleware = function (context, opts) { | ||
var proxy = httpProxy.createProxyServer(proxyOptions); | ||
console.log('[HPM] Proxy created:', config.context, ' -> ', proxyOptions.target); | ||
logger.info('[HPM] Proxy created:', config.context, ' -> ', proxyOptions.target); | ||
@@ -27,2 +28,7 @@ var pathRewriter = PathRewriter.create(proxyOptions.pathRewrite); // returns undefined when "pathRewrite" is not provided | ||
// Custom listener for the `proxyReq` event on `proxy`. | ||
if (_.isFunction(proxyOptions.onProxyReq)) { | ||
proxy.on('proxyReq', proxyOptions.onProxyReq); | ||
} | ||
// Custom listener for the `error` event on `proxy`. | ||
@@ -37,3 +43,3 @@ var onProxyError = getProxyErrorHandler(); | ||
// view disconnected websocket connections | ||
console.log('[HPM] Client disconnected'); | ||
logger.info('[HPM] Client disconnected'); | ||
}); | ||
@@ -57,2 +63,4 @@ | ||
if (contextMatcher.match(config.context, req.url)) { | ||
logger.debug('[HPM] Context match: "%s" -> "%s"', config.context, req.url); | ||
// handle option.pathRewrite | ||
@@ -62,7 +70,9 @@ if (pathRewriter) { | ||
} | ||
if (proxyOptions.proxyTable) { | ||
// change option.target when proxyTable present. | ||
proxy.web(req, res, ProxyTable.createProxyOptions(req, proxyOptions)); | ||
var altOpts = ProxyTable.createProxyOptions(req, proxyOptions); | ||
logger.debug('[HPM] Proxying "%s": "%s" -> "%s"', req.url, req.hostname, altOpts.target); | ||
proxy.web(req, res, altOpts); | ||
} else { | ||
logger.debug('[HPM] Proxying "%s": "%s" -> "%s"', req.url, req.hostname, proxyOptions.target); | ||
proxy.web(req, res); | ||
@@ -96,3 +106,3 @@ } | ||
proxy.ws(req, socket, head); | ||
console.log('[HPM] Upgrading to WebSocket'); | ||
logger.info('[HPM] Upgrading to WebSocket'); | ||
} | ||
@@ -111,3 +121,3 @@ } | ||
var targetUri = proxyOptions.target.host + req.url; | ||
console.log('[HPM] Proxy error:', err.code, targetUri); | ||
logger.error('[HPM] Proxy error: %s. %s -> "%s"', err.code, req.hostname, targetUri); | ||
} | ||
@@ -114,0 +124,0 @@ |
@@ -1,3 +0,4 @@ | ||
var _ = require('lodash'); | ||
var url = require('url'); | ||
var _ = require('lodash'); | ||
var url = require('url'); | ||
var logger = require('./logger').getInstance(); | ||
@@ -33,4 +34,3 @@ module.exports = { | ||
// Legacy option.proxyHost | ||
config.options = mapLegacyProxyHostOption(config.options); | ||
configureLogger(config.options); | ||
@@ -41,2 +41,5 @@ if (!config.options.target) { | ||
// Legacy option.proxyHost | ||
config.options = mapLegacyProxyHostOption(config.options); | ||
return config; | ||
@@ -54,7 +57,7 @@ }; | ||
if (options.proxyHost) { | ||
console.log('*************************************'); | ||
console.log('[HPM] Deprecated "option.proxyHost"'); | ||
console.log(' Use "option.changeOrigin" or "option.headers.host" instead'); | ||
console.log(' "option.proxyHost" will be removed in future release.'); | ||
console.log('*************************************'); | ||
logger.warn('*************************************'); | ||
logger.warn('[HPM] Deprecated "option.proxyHost"'); | ||
logger.warn(' Use "option.changeOrigin" or "option.headers.host" instead'); | ||
logger.warn(' "option.proxyHost" will be removed in future release.'); | ||
logger.warn('*************************************'); | ||
@@ -67,1 +70,11 @@ options.headers = options.headers || {}; | ||
} | ||
function configureLogger (options) { | ||
if (options.logLevel) { | ||
logger.setLevel(options.logLevel); | ||
} | ||
if (options.logProvider) { | ||
logger.setProvider(options.logProvider); | ||
} | ||
} |
@@ -1,2 +0,3 @@ | ||
var _ = require('lodash'); | ||
var _ = require('lodash'); | ||
var logger = require('./logger').getInstance(); | ||
@@ -26,2 +27,3 @@ module.exports = { | ||
result = result.replace(rule.regex, rule.value); | ||
logger.debug('[HPM] Rewriting path from "%s" to "%s"', path, result); | ||
return false; | ||
@@ -47,3 +49,3 @@ } | ||
}); | ||
console.log('[HPM] Proxy rewrite rule created: "' + key + '" -> "' + config[key] + '"'); | ||
logger.info('[HPM] Proxy rewrite rule created: "%s" -> "%s"', key, config[key]); | ||
}); | ||
@@ -50,0 +52,0 @@ |
@@ -1,3 +0,4 @@ | ||
var url = require('url'); | ||
var _ = require('lodash'); | ||
var url = require('url'); | ||
var _ = require('lodash'); | ||
var logger = require('./logger.js').getInstance(); | ||
@@ -15,2 +16,3 @@ module.exports = { | ||
if (newTarget) { | ||
logger.debug('[HPM] proxyTable new target: %s -> "%s"', config.target, newTarget); | ||
result = _.assign(result, {target : newTarget}); // override option.target | ||
@@ -35,2 +37,3 @@ } | ||
result = proxyTable[key]; | ||
logger.debug('[HPM] proxyTable match: %s -> "%s"', hostAndPath, result); | ||
return false; | ||
@@ -42,2 +45,3 @@ } | ||
result = proxyTable[key]; | ||
logger.debug('[HPM] proxyTable match: %s -> "%s"', host, result); | ||
return false; | ||
@@ -44,0 +48,0 @@ } |
{ | ||
"name": "http-proxy-middleware", | ||
"version": "0.8.2", | ||
"version": "0.9.0", | ||
"description": "The one-liner node.js proxy middleware for connect, express and browser-sync", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -166,2 +166,29 @@ # http-proxy-middleware | ||
* **option.logLevel**: string, ['debug', 'info', 'warn', 'error', 'silent']. Default: 'info' | ||
* **option.logProvider**: function, modify or replace log provider. Default: `console`. | ||
```javascript | ||
// simple replace | ||
function logProvider(provider) { | ||
// replace the default console log provider. | ||
return require('winston'); | ||
} | ||
``` | ||
```javascript | ||
// verbose replacement | ||
function logProvider(provider) { | ||
var logger = new (require('winston').Logger)(); | ||
var myCustomProvider = { | ||
log: logger.log, | ||
debug: logger.debug, | ||
info: logger.info, | ||
warn: logger.warn, | ||
error: logger.error | ||
} | ||
return myCustomProvider; | ||
} | ||
``` | ||
* **option.onError**: function, subscribe to http-proxy's error event for custom error handling. | ||
@@ -185,2 +212,11 @@ ```javascript | ||
* **option.onProxyReq**: function, subscribe to http-proxy's proxyReq event. | ||
```javascript | ||
function onProxyReq(proxyReq, req, res) { | ||
// add custom header to request | ||
proxyReq.setHeader('x-added', 'foobar'); | ||
// or log the req | ||
} | ||
``` | ||
* (DEPRECATED) **option.proxyHost**: Use `option.changeOrigin = true` instead. | ||
@@ -227,6 +263,6 @@ | ||
Or just explore the proxy examples' sources: | ||
* `examples/connect` - [connect proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/connect/index.js) | ||
* `examples/express` - [express proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/express/index.js) | ||
* `examples/browser-sync` - [browser-sync proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/browser-sync/index.js) | ||
* `examples/websocket` - [websocket proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/websocket/index.js) with express | ||
* `examples/connect` - [connect proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/connect/index.js) | ||
* `examples/express` - [express proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/express/index.js) | ||
* `examples/browser-sync` - [browser-sync proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/browser-sync/index.js) | ||
* `examples/websocket` - [websocket proxy example](https://github.com/chimurai/http-proxy-middleware/tree/master/examples/websocket/index.js) with express | ||
@@ -254,2 +290,4 @@ ## Compatible servers | ||
* [v0.9.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.9.0) - support subscribing to http-proxy proxyReq-event ([trbngr](https://github.com/trbngr)). Added logLevel and logProvider support. | ||
* [v0.8.2](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.8.2) - fixed proxyError handler ([mTazelaar](https://github.com/mTazelaar)) | ||
* [v0.8.1](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.8.1) - fixed pathRewrite when `agent` is configured | ||
@@ -256,0 +294,0 @@ * [v0.8.0](https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.8.0) - support external websocket upgrade, fixed websocket shorthand |
@@ -427,2 +427,40 @@ var expect = require('chai').expect; | ||
describe('option.onProxyReq', function () { | ||
var proxyServer, targetServer; | ||
var receivedRequest; | ||
beforeEach(function (done) { | ||
var fnOnProxyReq = function (proxyReq, req, res) { | ||
proxyReq.setHeader('x-added', 'foobar'); // add custom header to request | ||
}; | ||
var mw_proxy = proxyMiddleware('/api', { | ||
target: 'http://localhost:8000', | ||
onProxyReq: fnOnProxyReq | ||
}); | ||
var mw_target = function (req, res, next) { | ||
receivedRequest = req; | ||
res.write(req.url); // respond with req.url | ||
res.end(); | ||
}; | ||
proxyServer = createServer(3000, mw_proxy); | ||
targetServer = createServer(8000, mw_target); | ||
http.get('http://localhost:3000/api/foo/bar', function () { | ||
done(); | ||
}); | ||
}); | ||
afterEach(function () { | ||
proxyServer.close(); | ||
targetServer.close(); | ||
}); | ||
it('should add `x-added` as custom header to request"', function () { | ||
expect(receivedRequest.headers['x-added']).to.equal('foobar'); | ||
}); | ||
}); | ||
describe('option.pathRewrite', function () { | ||
@@ -531,2 +569,47 @@ var proxyServer, targetServer; | ||
describe('option.logLevel & option.logProvider', function () { | ||
var proxyServer, targetServer; | ||
var responseBody; | ||
var logMessage; | ||
beforeEach(function (done) { | ||
var customLogger = function (message) { | ||
logMessage = message; | ||
} | ||
var mw_proxy = proxyMiddleware('http://localhost:8000/api', { | ||
logLevel: 'info', | ||
logProvider: function (provider) { | ||
provider.debug = customLogger; | ||
provider.info = customLogger; | ||
return provider; | ||
} | ||
}); | ||
var mw_target = function (req, res, next) { | ||
res.write(req.url); // respond with req.url | ||
res.end(); | ||
}; | ||
proxyServer = createServer(3000, mw_proxy); | ||
targetServer = createServer(8000, mw_target); | ||
http.get('http://localhost:3000/api/foo/bar', function (res) { | ||
res.on('data', function (chunk) { | ||
responseBody = chunk.toString(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
afterEach(function () { | ||
proxyServer.close(); | ||
targetServer.close(); | ||
}); | ||
it('should have logged messages', function () { | ||
expect(logMessage).not.to.equal(undefined); | ||
}); | ||
}); | ||
}); | ||
@@ -539,3 +622,2 @@ | ||
if (middleware, path) { | ||
console.log('pathpathpathpathpathpathpath: ', path); | ||
app.use(path, middleware); | ||
@@ -542,0 +624,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
101627
27
1892
324