一切正常,很好,而不是一点,我在课后描述
首先,我的main.js,这是我的起点
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);
}
的proxy.js
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
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);
};
proxyhandler.js
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);
};
};
所以,当我第一次查询代理时,浏览器只是挂起,直到他抛出超时。 handle()在// Marker 1处调用 当我再次查询时,一切正常,当在//标记2
处调用handle()时我调试了,我能够调试,进入http代理,进入http并且无法找到问题。希望代码足以重播我的问题。
A,忘了....当我在proxy.js中调用proxyRequest()而没有委托给proxyhandler.js时,一切正常,即使在// Marker 1。