【问题标题】:NodeJS + http-proxy - Object has no method 'resume'NodeJS + http-proxy - 对象没有“恢复”方法
【发布时间】:2012-01-10 12:27:37
【问题描述】:

关于这个问题的奇怪之处在于,在我离开办公桌之前,我有一个工作示例(尽管握手后没有传输数据),但现在我似乎无法正常工作,尽管恢复我所做的一些小改动。

我正在使用 NodeJS + http-proxy 模块将 .html 文件的请求代理到 Apache,所有其他请求都由 NodeJS 处理。

这是我的 server.js 代码:

CometServer = (function() {

  var instance;

  function __construct() {

    var apachePort = 3000;
    var nodeJsPort = 3001;
    var apacheHost = 'localhost';
    var nodeJsHost = 'localhost';
    var httpProxy = require('/root/node_modules/http-proxy');
    var io = require('/root/node_modules/socket.io');
    var fs = require('/root/node/lib/fs');
    var path = require('/root/node/lib/path');
    var url = require('/root/node/lib/url');
    var apacheUrls = [
      /.*\.html/,
      /^\/$/
    ];

    function proxyRequests(request, response, proxy) {

      // Check if the URL is a comet request or not.
      var shouldProxy = apacheUrls.some(function(requestUrl) {
        return requestUrl.test(request.url);
      });

      // If 'request.url' matches any of the 'apacheUrls' then proxy to Apache.
      if (shouldProxy) {

        return proxy.proxyRequest(request, response, {
          host: apacheHost,
          port: apachePort
        });

      }

      // Not proxied to Apache; this is a comet request and should be processed by NodeJS.
      return handleUnproxiedRequests(request, response);

    }

    function handleUnproxiedRequests(request, response) {
      var uri = url.parse(request.url).pathname;
      var filePath = path.join(process.cwd(), '..', uri);

      var extname = path.extname(filePath);
      var contentType = 'text/javascript';

      path.exists(filePath, function(exists) {
        if (exists) {
          var fileStream = fs.createReadStream(filePath);
          fileStream.on('data', function (data) {
            response.writeHead(200, {
              'Content-Type': contentType
            });
            response.write(data);
            response.end();
          });
        }
        else {
          response.writeHead(404);
          response.end();
        }
      });
      return;
    }

    function bootstrap() {

      // Create a http proxy server and use the 'proxy' conditionally inside the request handler.
      var server = httpProxy.createServer(nodeJsPort, nodeJsHost, {
        target: {
          host: nodeJsHost,
          port: nodeJsPort
        }
      }, proxyRequests);

      // Get the http proxy server to listen to a port.
      server.listen(nodeJsPort);

      // Get socket.io to listen to the proxy server.
      var listener = io.listen(server);

      var something = listener.on('connection', function(socket) {
        console.log('socket connected');
        socket.on('handshake', function(pid) {
          console.log('socket? ' + pid);
        })
      });

    }

    // Bootstrap server.
    bootstrap();

    return { }

  }

  return {
    getInstance: function() {
      // Instantiate only if the instance doesn't already exist.
      if (!instance) {
        instance = __construct();
      }
      return instance;
    }
  }
})();

// Initialize the comet server.
CometServer.getInstance();

还有客户端js:

var socket = io.connect();
console.log('Connected');
socket.on('connect', function(data) {
  console.log('Sending handshake');
  socket.emit('handshake', 1);
});

请注意,此代码目前有点混乱,因为它处于生产的早期阶段。但是,如果您愿意,请随意批评它,如果您认为我可以/应该以不同的方式做某事。

这是我在尝试访问 .html 页面时收到的错误消息:

/root/node_modules/http-proxy/lib/node-http-proxy/http-proxy.js:334
      ? buffer.resume()
           ^
TypeError: Object #<Object> has no method 'resume'
    at [object Function].proxyRequest (/root/node_modules/http-proxy/lib/node-http-proxy/http-proxy.js:334:16)
    at proxyRequests (/var/www/html/Comet/server.js:139:22)
    at Server.<anonymous> (/root/node_modules/http-proxy/lib/node-http-proxy.js:321:7)
    at Manager.handleRequest (/root/node_modules/socket.io/lib/manager.js:531:28)
    at Server.<anonymous> (/root/node_modules/socket.io/lib/manager.js:112:10)
    at Server.emit (events.js:70:17)
    at HTTPParser.onIncoming (http.js:1479:12)
    at HTTPParser.onHeadersComplete (http.js:102:31)
    at Socket.ondata (http.js:1375:22)
    at TCP.onread (net.js:334:27)

根据我的阅读,看起来数据是缓冲的而不是流式的,但我真的不知道如何解决这个问题,因为我对 API 很陌生(这是我的第一个 NodeJS 项目)。

任何帮助将不胜感激,因为我已经用这个把头撞在砖墙上很长一段时间了。

【问题讨论】:

  • 我也试过故意将缓冲区传递给proxyRequest:如下: var buffer = httpProxy.buffer(request); return proxy.proxyRequest(request, response, { port: apachePort, host: apacheHost, buffer: buffer });不幸的是,我仍然得到相同的结果。

标签: javascript ajax node.js comet http-proxy


【解决方案1】:

嗯,似乎传递给 createServer 方法的一些参数是无关的 - 我用以下方法解决了这个问题:

var server = httpProxy.createServer(proxyRequests);

我不完全确定为什么会这样,以及为什么我之前的示例在最初正常运行后停止工作。

【讨论】:

    【解决方案2】:

    我认为您遇到了这个问题: https://github.com/joyent/node/issues/1956

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-17
      • 2019-04-01
      • 1970-01-01
      相关资源
      最近更新 更多