Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Problem: async. Proxyrequest #340

xFragger opened this Issue Nov 26, 2012 · 1 comment


None yet
1 participant


i despair. I think, this isn't big problem, but i can't find the problem.
I think, the best is, when i just post my classes and describe the problem, i've got. I stripped down the functionality, so the most i do will seem useless, but believe me, without stripped down code, it is useful :)

Everything works, fine, instead of the one point, i describe after the classes

First, my main.js, which is my startpoint

var proxy = require('./proxy');
var mongo = require('mongodb');
var mongoserver = new mongo.Server('localhost', 27017, {auto_reconnect:true});
var mongodb = new mongo.Db('proxy', mongoserver, {safe:false});
mongodb.open(function(err, db) {
    proxy.createServer(8000, db);


var httpProxy = require('./node_modules/http-proxy/lib/node-http-proxy');
var proxyhandler = require('./proxyhandler');

exports.createServer = function(port, mongodb) {
    return new Proxy(port, mongodb);

var Proxy = exports.Proxy = function(port, mongodb) {
    var handlers = {};
    var requestCallback = function(req, res, proxy) {
        var host = req.headers.host; //target host, find user modules by this + log for him
        if (!handlers[host]) {
            mongodb.collection('customerConfig', function(err, collection) {
                collection.findOne({host:host}, function(err, item) {
                    handlers[host] = proxyhandler.createHandler(mongodb, item);
                    handers[host].handle(req, res, proxy); //Marker 1
        } else {
            handers[host].handle(req, res, proxy); //Marker 2

    return httpProxy.createServer(requestCallback).listen(port);


exports.createHandler = function (mongodb, item) {
    return new Handler(mongodb, item);

var Handler = function(mongodb, config) {
    this.handle = function (req, res, proxy) {
        var target = { host : 'google.de', port : 80 }; //normally this is set by "item"
        proxy.proxyRequest(req, res, target);

so, when i query the proxy the first time, the browser just hangs, till he throws a timeout. handle() is called at //Marker 1
when i query again, everything works fine, when handle() is called at //Marker 2

I debugged, what i was able to debug, stepped into http-proxy, to http and was not able to find the problem. hope the code is enough to replay my problem.

A, forgot.... when i call proxyRequest() in proxy.js, without delegating to proxyhandler.js, everything works fine, even at //Marker 1.

okay, solved -.-
forgot to buffer the data/end events, like in the example "latent-proxy"

sorry for that

@xFragger xFragger closed this Nov 28, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment