@@ -3,7 +3,33 @@ var historyApiFallback = require('connect-history-api-fallback');
33var httpProxyMiddleware = require ( 'http-proxy-middleware' ) ;
44var paths = require ( '../../config/paths' ) ;
55
6- module . exports = function addMiddleware ( devServer ) {
6+ // We need to provide a custom onError function for httpProxyMiddleware.
7+ // It allows us to log custom error messages on the console.
8+ function onProxyError ( proxy ) {
9+ return function ( err , req , res ) {
10+ var host = req . headers && req . headers . host ;
11+ console . log (
12+ chalk . red ( 'Proxy error:' ) + ' Could not proxy request ' + chalk . cyan ( req . url ) +
13+ ' from ' + chalk . cyan ( host ) + ' to ' + chalk . cyan ( proxy ) + '.'
14+ ) ;
15+ console . log (
16+ 'See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (' +
17+ chalk . cyan ( err . code ) + ').'
18+ ) ;
19+ console . log ( ) ;
20+
21+ // And immediately send the proper error response to the client.
22+ // Otherwise, the request will eventually timeout with ERR_EMPTY_RESPONSE on the client side.
23+ if ( res . writeHead && ! res . headersSent ) {
24+ res . writeHead ( 500 ) ;
25+ }
26+ res . end ( 'Proxy error: Could not proxy request ' + req . url + ' from ' +
27+ host + ' to ' + proxy + ' (' + err . code + ').'
28+ ) ;
29+ }
30+ }
31+
32+ module . exports = function addWebpackMiddleware ( devServer ) {
733 // `proxy` lets you to specify a fallback server during development.
834 // Every unrecognized request will be forwarded to it.
935 var proxy = require ( paths . appPackageJson ) . proxy ;
@@ -54,7 +80,8 @@ module.exports = function addMiddleware(devServer) {
5480 onError : onProxyError ( proxy ) ,
5581 secure : false ,
5682 changeOrigin : true ,
57- ws : true
83+ ws : true ,
84+ xfwd : true
5885 } ) ;
5986 devServer . use ( mayProxy , hpm ) ;
6087
@@ -68,29 +95,3 @@ module.exports = function addMiddleware(devServer) {
6895 // It may be /index.html, so let the dev server try serving it again.
6996 devServer . use ( devServer . middleware ) ;
7097} ;
71-
72- // We need to provide a custom onError function for httpProxyMiddleware.
73- // It allows us to log custom error messages on the console.
74- function onProxyError ( proxy ) {
75- return function ( err , req , res ) {
76- var host = req . headers && req . headers . host ;
77- console . log (
78- chalk . red ( 'Proxy error:' ) + ' Could not proxy request ' + chalk . cyan ( req . url ) +
79- ' from ' + chalk . cyan ( host ) + ' to ' + chalk . cyan ( proxy ) + '.'
80- ) ;
81- console . log (
82- 'See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (' +
83- chalk . cyan ( err . code ) + ').'
84- ) ;
85- console . log ( ) ;
86-
87- // And immediately send the proper error response to the client.
88- // Otherwise, the request will eventually timeout with ERR_EMPTY_RESPONSE on the client side.
89- if ( res . writeHead && ! res . headersSent ) {
90- res . writeHead ( 500 ) ;
91- }
92- res . end ( 'Proxy error: Could not proxy request ' + req . url + ' from ' +
93- host + ' to ' + proxy + ' (' + err . code + ').'
94- ) ;
95- }
96- }
0 commit comments