From d4a525f682f82cc9c48a7b8bd28ff885cc5c171b Mon Sep 17 00:00:00 2001 From: Alex DeNeui Date: Wed, 9 Sep 2015 16:56:32 -0700 Subject: [PATCH] Conditionally write to the console when proxy events occur --- README.md | 1 + index.js | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 554a0af3..ab869a21 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,7 @@ var server = app.listen(3000); delete proxyRes.headers['x-removed']; // remove header from response } ``` +* **option.debugLog**: boolean, log to console when proxy-related events happen. * (DEPRECATED) **option.proxyHost**: Use `option.changeOrigin = true` instead. diff --git a/index.js b/index.js index ccfabe34..028f1514 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,7 @@ var httpProxyMiddleware = function (context, opts) { // create proxy var proxy = httpProxy.createProxyServer(proxyOptions); - console.log('[HPM] Proxy created:', config.context, ' -> ', proxyOptions.target); + debugLog('[HPM] Proxy created:', config.context, ' -> ', proxyOptions.target); var pathRewriter = PathRewriter.create(proxyOptions.pathRewrite); // returns undefined when "pathRewrite" is not provided @@ -31,7 +31,7 @@ var httpProxyMiddleware = function (context, opts) { // Listen for the `close` event on `proxy`. proxy.on('close', function (req, socket, head) { // view disconnected websocket connections - console.log('[HPM] Client disconnected'); + debugLog('[HPM] Client disconnected'); }); // https://github.com/chimurai/http-proxy-middleware/issues/19 @@ -71,6 +71,12 @@ var httpProxyMiddleware = function (context, opts) { } } + function debugLog () { + if (proxyOptions.debugLog) { + console.log.apply(null, arguments); + } + } + function catchUpgradeRequest (server) { // make sure only 1 handle listens to server's upgrade request. if (isWsUpgradeListened === true) { @@ -87,7 +93,7 @@ var httpProxyMiddleware = function (context, opts) { req.url = pathRewriter(req.url); } proxy.ws(req, socket, head); - console.log('[HPM] Upgrading to WebSocket'); + debugLog('[HPM] Upgrading to WebSocket'); } } @@ -101,7 +107,7 @@ var httpProxyMiddleware = function (context, opts) { function proxyErrorLogger (err, req, res) { var targetUri = proxyOptions.target.host + req.url; - console.log('[HPM] Proxy error:', err.code, targetUri); + debugLog('[HPM] Proxy error:', err.code, targetUri); } };