I'm using http-proxy-middleware
to proxy some API endpoints to my Create React App development server.
I recently introduced a WebSocket endpoint, and I'm proxying it with the following code in setupProxy.js
:
const proxy = require('http-proxy-middleware');
const target = 'http://localhost:8000';
module.exports = function(app) {
[...] // other proxies for HTTP endpoints
app.use(proxy('/api/ws', { ws: true, target }));
};
The problem I'm having is that if I restart the backend, and the WebSocket connection is interrupted, the whole development server crashes with:
[HPM] Upgrading to WebSocket
events.js:170
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:171:27)
Emitted 'error' event at:
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at processTicksAndRejections (internal/process/task_queues.js:81:17)
error Command failed with exit code 1.
I was wondering how could I gracefully handle errors in the WebSocket proxy so that the server won't crash, but will wait for the WebSocket to be available again instead?