带有lite-server和http-proxy-middleware的websockets

时间:2016-11-23 14:56:24

标签: node.js lite-server http-proxy-middleware

在我的开发环境中,我必须代理请求以及与相应后端服务的websocket连接。 为此,我使用了lite-serverhttp-proxy-middleware

我的精简服务器配置文件如下所示:

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
            } 
        }
    };
};

但是我总是在我的控制台中收到此错误:

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

在lite-server的控制台输出中,我执行以下操作:

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

我甚至看到连接通过我的节点端点,因为我看到了控制台输出。端点实现如下:

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");
}

出现此错误的原因是什么?我的配置有问题吗?

0 个答案:

没有答案