1

In my development environment I have to proxy requests as well as websocket connections to the respective backend service. For this I am using lite-server and http-proxy-middleware.

My lite server config file looks like this:

var proxyMiddleware = require('http-proxy-middleware');

var oProxyOptions = {
    target: 'http://localhost:3000/api',
    ws: true,
    pathRewrite: function (path, req) { return path.replace('/api', ''); },
    changeOrigin: true
};

var oProxy = proxyMiddleware('/api', oProxyOptions);

module.exports = function(bs) {
    return {
        "port": 8000,
        "files": ["./resources/**/*.{html,htm,css,js,xml}"],
        "server": { 
            "baseDir": "./resources",
            "middleware": {
                1: oProxy
            } 
        }
    };
};

However I always receive this error in my console:

WebSocket connection to 'ws://localhost:8000/api/ws' failed: Connection closed before receiving a handshake response

In lite-server's console output I do the following entry:

[HPM] GET /api/ws ~> http://localhost:3000/api/ws
[HPM] Upgrading to WebSocket 

I even see that the connection makes it through to my node endpoint as I see a console output. Endpoint is implemented as follows:

var ws  = require('ws').Server;
var wss = new ws({ server: server, path:'/api/ws' });
wss.on('connection', function connection(ws) {
    var location = url.parse(ws.upgradeReq.url, true);
    console.log("New WS connection");
}

What could be the reason for this error? Is there something wrong in my configuration?

newBee
  • 1,289
  • 1
  • 14
  • 31

0 Answers0