| 261 | // We need to provide a custom onError function for httpProxyMiddleware. |
| 262 | // It allows us to log custom error messages on the console. |
| 263 | function onProxyError(proxy) { |
| 264 | return (err, req, res) => { |
| 265 | const host = req.headers && req.headers.host; |
| 266 | console.log( |
| 267 | chalk.red('Proxy error:') + |
| 268 | ' Could not proxy request ' + |
| 269 | chalk.cyan(req.url) + |
| 270 | ' from ' + |
| 271 | chalk.cyan(host) + |
| 272 | ' to ' + |
| 273 | chalk.cyan(proxy) + |
| 274 | '.' |
| 275 | ); |
| 276 | console.log( |
| 277 | 'See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (' + |
| 278 | chalk.cyan(err.code) + |
| 279 | ').' |
| 280 | ); |
| 281 | console.log(); |
| 282 | |
| 283 | // And immediately send the proper error response to the client. |
| 284 | // Otherwise, the request will eventually timeout with ERR_EMPTY_RESPONSE on the client side. |
| 285 | if (res.writeHead && !res.headersSent) { |
| 286 | res.writeHead(500); |
| 287 | } |
| 288 | res.end( |
| 289 | 'Proxy error: Could not proxy request ' + |
| 290 | req.url + |
| 291 | ' from ' + |
| 292 | host + |
| 293 | ' to ' + |
| 294 | proxy + |
| 295 | ' (' + |
| 296 | err.code + |
| 297 | ').' |
| 298 | ); |
| 299 | }; |
| 300 | } |
| 301 | |
| 302 | function prepareProxy(proxy, appPublicFolder, servedPathname) { |
| 303 | // `proxy` lets you specify alternate servers for specific requests. |