Skip to content

Commit a79c38c

Browse files
cloudmufeiqitian
authored andcommitted
Provide custom onError handler for http-proxy-middleware (facebook#502)
* Change http-proxy-middleware logLevel from silent to error * provide onError handler for httpProxyMiddleware
1 parent e0f4e61 commit a79c38c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

scripts/start.js

+18
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,23 @@ function openBrowser(port, protocol) {
170170
opn(protocol + '://localhost:' + port + '/');
171171
}
172172

173+
// We need to provide a custom onError function for httpProxyMiddleware.
174+
// It allows us to log custom error messages on the console.
175+
function onProxyError(proxy) {
176+
return function(err, req, res){
177+
var host = req.headers && req.headers.host;
178+
console.log(
179+
chalk.red('Proxy error:') + ' Could not proxy request ' + chalk.cyan(req.url) +
180+
' from ' + chalk.cyan(host) + ' to ' + chalk.cyan(proxy) + '.'
181+
);
182+
console.log(
183+
'See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (' +
184+
chalk.cyan(err.code) + ').'
185+
);
186+
console.log();
187+
}
188+
}
189+
173190
function addMiddleware(devServer) {
174191
// `proxy` lets you to specify a fallback server during development.
175192
// Every unrecognized request will be forwarded to it.
@@ -209,6 +226,7 @@ function addMiddleware(devServer) {
209226
httpProxyMiddleware(pathname => mayProxy.test(pathname), {
210227
target: proxy,
211228
logLevel: 'silent',
229+
onError: onProxyError(proxy),
212230
secure: false,
213231
changeOrigin: true
214232
})

0 commit comments

Comments
 (0)