node.js node-http-proxy:在 proxy.web 中定位时如何避免 WebSocket 异常

node.js node-http-proxy: How to avoid exceptions with WebSocket when target in proxy.web

在 node.js 中使用 http-proxy(又名 node-http-proxy),我无法弄清楚如何在动态确定目标时(即处理请求时)代理 Web 套接字。

查看文档: https://github.com/nodejitsu/node-http-proxy

有一个示例可以在处理请求时设置目标:

var http = require('http'),
    httpProxy = require('http-proxy');

//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createProxyServer({});

//
// Create your custom server and just call `proxy.web()` to proxy
// a web request to the target passed in the options
// also you can use `proxy.ws()` to proxy a websockets request
//
var server = http.createServer(function(req, res) {
  // You can define here your custom logic to handle the request
  // and then proxy the request.
  proxy.web(req, res, { target: 'http://127.0.0.1:5060' });
});

console.log("listening on port 5050")
server.listen(5050);

下面还有另一个示例显示通过 proxy.ws() 支持 websockets,但它显示目标是静态设置的,而不是根据请求设置的:

//
// Setup our server to proxy standard HTTP requests
//
var proxy = new httpProxy.createProxyServer({
  target: {
    host: 'localhost',
    port: 9015
  }
});
var proxyServer = http.createServer(function (req, res) {
  proxy.web(req, res);
});

//
// Listen to the `upgrade` event and proxy the
// WebSocket requests as well.
//
proxyServer.on('upgrade', function (req, socket, head) {
  proxy.ws(req, socket, head);
});

proxyServer.listen(8015);

我采用了第一个示例并添加了第二个示例中的 proxyServer.on('upgrade'... proxy.ws() ... 内容以获得设置的示例处理请求时的目标,也支持websockets。HTTP网页似乎工作正常,但在处理websocket请求时抛出异常。

'use strict';
var http = require('http'),
    httpProxy = require('http-proxy');

//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createProxyServer({});

//
// Create your custom server and just call `proxy.web()` to proxy
// a web request to the target passed in the options
// also you can use `proxy.ws()` to proxy a websockets request
//
var server = http.createServer(function(req, res) {
  // You can define here your custom logic to handle the request
  // and then proxy the request.
  proxy.web(req, res, { target: 'http://127.0.0.1:5060' });
});

//
// Listen to the `upgrade` event and proxy the
// WebSocket requests as well.
//
server.on('upgrade', function (req, socket, head) {
  proxy.ws(req, socket, head);
});

console.log("listening on port 5050")
server.listen(5050);

异常发生在 proxy.ws(req, socket, head) 调用中:

Error: Must provide a proper URL as target
    at ProxyServer.<anonymous> (...../node_modules/http-proxy/lib/http-proxy/index.js:68:35)
    at Server.<anonymous> (...../poc.js:26:9)  // the location in my sample code of the proxy.ws(req, socket, head) above
    at emitThree (events.js:116:13)
    at Server.emit (events.js:194:7)
    at onParserExecuteCommon (_http_server.js:409:14)
    at HTTPParser.onParserExecute (_http_server.js:377:5)

如果选项中没有 .target 或 .forward 成员,http-proxy/index.js:68:35 中的代码将抛出此异常。

如何在每个请求的基础上设置目标并让 websockets 工作?

我有一个答案。在看了康拉德的这个问题和评论之后,然后进行了实验: single-proxyserver-to-multiple-targets

proxy.ws 可以接受额外的选项参数,就像 proxy.web.

这是工作代码。

'use strict';
var http = require('http'),
    httpProxy = require('http-proxy');

//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createProxyServer({});

//
// Create your custom server and just call `proxy.web()` to proxy
// a web request to the target passed in the options
// also you can use `proxy.ws()` to proxy a websockets request
//
var server = http.createServer(function(req, res) {
  // You can define here your custom logic to handle the request
  // and then proxy the request.
  proxy.web(req, res, { target: 'http://127.0.0.1:5060' });
});

//
// Listen to the `upgrade` event and proxy the
// WebSocket requests as well.
//
server.on('upgrade', function (req, socket, head) {
  proxy.ws(req, socket, head, { target: 'ws://127.0.0.1:5060' });
});

console.log("listening on port 5050")
server.listen(5050);