1

考虑到这个不完整的片段:

var util = require('util'),
    nconf = require('nconf'),

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

    express = require('express'),
    repoServer = express.createServer(),

    redis = require('redis'),
    redisClient = redis.createClient();

// (...)

var proxy = new httpProxy.RoutingProxy();
http.createServer(function (req, res) {
  console.log("URL", req.url);
  if (req.url) {
    var token = req.url.split("/")[1];

    // if I leave this code here it works fine
    // var target = { host: 'local-01', port: 8024 }
    // proxy.proxyRequest(req, res, target);

    // now I need to retrieve some routing information
    // from redis, so I query redis here
    redisClient.get(token, function (err, reply) {
      // if I leave this code here the request hangs
      var target = { host: 'local-01', port: 8024 }
      proxy.proxyRequest(req, res, target);
    });
  }
}).listen(routerInfo.port, routerInfo.address);

为什么当我调用proxyRequest外部 de redis 客户端get回调时它可以工作,但是当我在回调中移动调用时它失败并且 HTTP 请求只是挂起?

4

1 回答 1

2

请求是流,也许你想缓冲

见:https ://github.com/nodejitsu/node-http-proxy/blob/master/examples/http/latent-proxy.js

于 2012-04-16T00:45:36.347 回答