From e54b8c1ea91b08463317734aea96c05c5bcd262e Mon Sep 17 00:00:00 2001 From: Yunjun Mu Date: Fri, 26 Aug 2016 13:57:23 -0400 Subject: [PATCH 1/3] Change http-proxy-middleware logLevel from silent to error --- scripts/start.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/start.js b/scripts/start.js index aa68ab40ff3..f65d8d85cb5 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -206,7 +206,7 @@ function addMiddleware(devServer) { // of both HTTP and WebSockets to work without false positives. httpProxyMiddleware(pathname => mayProxy.test(pathname), { target: proxy, - logLevel: 'silent', + logLevel: 'error', secure: false, changeOrigin: true }) From 4dd0d43cf05c836f8659606d73762d331aabe266 Mon Sep 17 00:00:00 2001 From: yunjun Date: Fri, 2 Sep 2016 20:54:55 -0400 Subject: [PATCH 2/3] provide onError handler for httpProxyMiddleware --- scripts/start.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/scripts/start.js b/scripts/start.js index f33e08326cf..a0b01545b5b 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -170,6 +170,23 @@ function openBrowser(port, protocol) { opn(protocol + '://localhost:' + port + '/'); } +// We need to provide a custom onError function for httpProxyMiddleware. +// It allows us to log custom error messages on the console. +function onProxyError(proxy) { + return function(err, req, res){ + var host = req.headers && req.headers.host; + console.log( + chalk.red('Proxy error:') + ' Could not proxy request ' + chalk.cyan(req.url) + + ' from ' + chalk.cyan(host) + ' to ' + chalk.cyan(proxy) + '.' + ); + console.log( + 'See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (' + + chalk.cyan(err.code) + ').' + ); + console.log(); + } +} + function addMiddleware(devServer) { // `proxy` lets you to specify a fallback server during development. // Every unrecognized request will be forwarded to it. @@ -208,7 +225,8 @@ function addMiddleware(devServer) { // of both HTTP and WebSockets to work without false positives. httpProxyMiddleware(pathname => mayProxy.test(pathname), { target: proxy, - logLevel: 'error', + logLevel: 'silent', + onError: onProxyError(proxy), secure: false, changeOrigin: true }) From 1079a6ef7c41af8dae783b92563a70f441ae2f79 Mon Sep 17 00:00:00 2001 From: yunjun Date: Mon, 5 Sep 2016 12:30:25 -0400 Subject: [PATCH 3/3] Send proper error reponse upon proxy error. --- scripts/start.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/scripts/start.js b/scripts/start.js index a0b01545b5b..667a4b8f440 100644 --- a/scripts/start.js +++ b/scripts/start.js @@ -175,6 +175,8 @@ function openBrowser(port, protocol) { function onProxyError(proxy) { return function(err, req, res){ var host = req.headers && req.headers.host; + + // Log the error to the deveopment console. console.log( chalk.red('Proxy error:') + ' Could not proxy request ' + chalk.cyan(req.url) + ' from ' + chalk.cyan(host) + ' to ' + chalk.cyan(proxy) + '.' @@ -184,6 +186,15 @@ function onProxyError(proxy) { chalk.cyan(err.code) + ').' ); console.log(); + + // And immediately send the proper error response to the client. + // Otherwise, the request will eventually timeout with ERR_EMPTY_RESPONSE on the client side. + if (res.writeHead && !res.headersSent) { + res.writeHead(500); + } + res.end('Proxy error: Could not proxy request ' + req.url + ' from ' + + host + ' to ' + proxy + ' (' + err.code + ').' + ); } }