使用节点代理时出错

我使用下面的程序,当我运行它我有以下错误我能够运行应用程序,但是当我把浏览器localhost:3000我在控制台中得到这个错误…

**Error: connect ECONNREFUSED** at exports._errnoException (util.js:746:11) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1000:19) 

这是的程序非常简单的节点applicaton只有一个文件,下面的代码(这是我的服务器/应用程序/ .js

 var http = require('http'), httpProxy = require('http-proxy'), url = require('url'); proxy = httpProxy.createProxyServer({}); http.createServer(function (req, res) { switch (hostname) { case 'localhost': proxy.web(req, res, {target: 'http://localhost:9001'}); break; } }).listen(3005, function () { console.log('original proxy listening on port: ' + 3005); }); http.createServer(function(req, res) { res.end("Request received on 9001"); }).listen(9056); 

当用户点击某个URL时,我想启动新的代理服务器

我使用这个模块,我做什么错了?

https://github.com/nodejitsu/node-http-proxy

另一件事…当我使用这个代码,我得到错误…

 process.on('uncaughtException', function (err) { console.log(err); }); 

这是现在的错误,任何想法?

 { [Error: connect ECONNREFUSED] code: 'ECONNREFUSED', errno: 'ECONNREFUSED', syscall: 'connect' } { [Error: socket hang up] code: 'ECONNRESET' } 

 http.createServer(function(req, res) { res.end("Request received on 9001"); }).listen(9056); 

您的HTTP服务器正在侦听端口9056.当无法build立连接时,代理尝试连接到端口错误的HTTP服务器并引发错误。 为了避免将来出现这样的错误,将port放在一个variables中:

 var PORT = 9001; http.createServer(function(req, res) { res.end("Request received on " + PORT); }).listen(PORT);