【问题标题】:how to do a proxy for a websocket in Node如何在 Node 中为 websocket 做代理
【发布时间】:2018-05-24 04:45:18
【问题描述】:

在 Node 中,我可以通过以下方式执行 http 代理:

https://localhost:443 -> http://localhost:8100

使用以下代码:

var express = require('express');
var app = express();
var https = require('https');
var httpProxy = require('http-proxy');
...
app.use(function (req, res, next) {
    httpProxy.createServer({
        target: {
            host: 'localhost',
            port: 8100,
        }
    }).web(req, res);
});
...
https.createServer(sslOptions, app).listen(443, function(){
    console.log('App running on port: 443');
});

但我还需要通过以下方式对 Web 套接字进行类似的重定向:

wss://localhost:8107 -> ws://localhost:8100

您对我该如何实现这一点有任何想法吗?

谢谢!

【问题讨论】:

标签: node.js npm websocket middleware


【解决方案1】:

http-proxy 模块 supports websockets

只要做:

httpProxy.createServer({
  target: 'ws://localhost:8100',
  ws: true
}).listen(8107);

【讨论】:

    猜你喜欢
    • 2014-04-19
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 2020-08-07
    相关资源
    最近更新 更多